jskatas.org Continuously Learn JavaScript. Your Way.

Arrow functions: function binding

Arrow functions have lexical this, no dynamic this.

Arrow functions have lexical this, no dynamic this

bound at definition time, use =>
class LexicallyBound { getFunction() { return () => { return new LexicallyBound(); } } } var bound = new LexicallyBound(); var fn = bound.getFunction(); assert.strictEqual(fn(), bound);
can NOT bind a different context
class LexicallyBound { getFunction() { return () => { return new LexicallyBound(); } } } var bound = new LexicallyBound(); var fn = bound.getFunction(); var anotherObj = {}; var expected = anotherObj; assert.strictEqual(fn.call(anotherObj), expected);
arguments does NOT work inside arrow functions
class LexicallyBound { getArgumentsFunction() { return function() {return arguments} } } var bound = new LexicallyBound(); var fn = bound.getArgumentsFunction(); assert.equal(fn(1, 2).length, 0);

Required Knowledge

Related Katas

function API

Arrow functions

Async Function

Difficulty Level

BEGINNER

First Published

20 March 2015

Stats

3 tests to solve