I am learning ECMAScript6 in es6katas.org, which is great and highly recommended. I am currently stuck at this pretty basic kata about arrow function. I can't seem to understand what the author meant in the second test:
class LexicallyBound {
getFunction() {
return () => {
return new LexicallyBound();
}
}
getArgumentsFunction() {
return function() {return arguments}
}
}
it('bound at definition time, use `=>` ', function() {
var bound = new LexicallyBound();
var fn = bound.getFunction();
assert.strictEqual(fn(), bound);
});
Can someone assist in figuring it out?
I think it wanted you to change getFunction
to return this
.
Reason: Fat arrow function's this
is bounded to the defining context (the bound
object) when it was defined - when bound
was created - instead of having a dynamic this
like normal functions.