Skip to content

Instantly share code, notes, and snippets.

@reneviering
Last active June 26, 2018 13:10
Show Gist options
  • Save reneviering/5147a647f5f540797003938186a74817 to your computer and use it in GitHub Desktop.
Save reneviering/5147a647f5f540797003938186a74817 to your computer and use it in GitHub Desktop.
// 6: arrow functions - binding
// To do: make all tests pass, leave the asserts unchanged!
class LexicallyBound {
getFunction() {
return () => this
}
getArgumentsFunction() {
return () => arguments
}
}
describe('arrow functions have lexical `this`, no dynamic `this`', () => {
it('bound at definition time, use `=>` ', function() {
var bound = new LexicallyBound();
var fn = bound.getFunction();
assert.strictEqual(fn(), bound);
});
it('can NOT bind a different context', function() {
var bound = new LexicallyBound();
var fn = bound.getFunction();
var anotherObj = {};
var expected = bound;
assert.strictEqual(fn.call(anotherObj), expected);
});
it('`arguments` doesnt work inside arrow functions', function() {
var bound = new LexicallyBound();
var fn = bound.getArgumentsFunction();
assert.equal(fn(1, 2).length, 0);
});
});
@reneviering
Copy link
Author

Challenge 6/79 (arrow functions - binding)

This kata was a little more difficult. I wondered about the structure of the arguments object in the context of an arrow function. After i played with this topic on jsbin, i got the test green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment