Created
March 1, 2017 09:52
-
-
Save underwindfall/c91d6c545811dc68505d4fe956dbe2a4 to your computer and use it in GitHub Desktop.
Answers of Es6katas [http://es6katas.org/]
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
function.length will return number of arguments which are not defined with default values
Exemple:(function(...args) {}).length // 0