I was messing around with trying to chain methods and realized things weren't working in my example.
var object = {
method1: () => {
console.log('method1');
},
method2: () => {
console.log('method2');
}
};
object.method1(); // works fine
object.method2(); // works fine
object.method1().method2(); // merp
I know arrow functions do not have its own this
context so the situation that is happening above is the window
context by default is being set and when the second method tries to trigger, it fails because it tries to find method2
on the window
object (please correct me if this is the wrong interpretation of what is happening) and in order to achieve method chaining you have to return this
so naturally, this is a problem.
I know the simple solution is simple to use function()
instead of arrow functions in the case above, but I was wondering if there was a way to achieve method chaining with arrow functions or is this simply not possible?
Related questions I've read: Arrow function vs function declaration / expressions: Are they equivalent / exchangeable?