Skip to content

Instantly share code, notes, and snippets.

@underwindfall
Created March 1, 2017 09:52
Show Gist options
  • Save underwindfall/c91d6c545811dc68505d4fe956dbe2a4 to your computer and use it in GitHub Desktop.
Save underwindfall/c91d6c545811dc68505d4fe956dbe2a4 to your computer and use it in GitHub Desktop.
Answers of Es6katas [http://es6katas.org/]
// 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);
});
});
@underwindfall
Copy link
Author

function.length will return number of arguments which are not defined with default values
Exemple:(function(...args) {}).length // 0

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