Last active
June 26, 2018 13:10
-
-
Save reneviering/5147a647f5f540797003938186a74817 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.