Last active
December 26, 2017 07:52
-
-
Save hanmd82/fb0d05d6449d08eeb9670b178ec31ee0 to your computer and use it in GitHub Desktop.
ES6 katas for Arrow functions - 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
// 5: arrow functions - basics | |
describe('arrow functions', function() { | |
it('are shorter to write', function() { | |
var func = () => { | |
return 'I am func'; | |
}; | |
assert.equal(func(), 'I am func'); | |
}); | |
it('a single expression, without curly braces returns too', function() { | |
var func = () => 'I return too'; | |
assert.equal(func(), 'I return too'); | |
}); | |
it('one parameter can be written without parens', () => { | |
var func = p => p - 1; | |
assert.equal(func(25), 24); | |
}); | |
it('many params require parens', () => { | |
var func = (param, param1) => param + param1; | |
assert.equal(func(23, 42), 23+42); | |
}); | |
it('body needs parens to return an object', () => { | |
var func = () => { | |
return {iAm: 'an object'} | |
}; | |
assert.deepEqual(func(), {iAm: 'an object'}); | |
}); | |
}); | |
// 6: arrow functions - binding | |
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