Created
December 27, 2017 06:46
-
-
Save hanmd82/67d954e85cdec70489dacda640546d8c to your computer and use it in GitHub Desktop.
ES6 katas for default parameters - http://es6katas.org/
This file contains hidden or 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
// 57: Default parameters - basics | |
describe('default parameters make function parameters more flexible', () => { | |
it('define it using an assignment to the parameter `function(param=1){}`', function() { | |
let number = (int = 0) => int; | |
assert.equal(number(), 0); | |
}); | |
it('it is used when undefined is passed', function() { | |
let number = (int = 23) => int; | |
const param = undefined; | |
assert.equal(number(param), 23); | |
}); | |
it('it is not used when a value is given', function() { | |
function xhr(method='GET') { | |
return method; | |
} | |
assert.equal(xhr('POST'), 'POST'); | |
}); | |
it('it is evaluated at run time', function() { | |
let defaultValue=42; | |
function xhr(method = `value: ${defaultValue}`) { | |
return method; | |
} | |
assert.equal(xhr(), 'value: 42'); | |
defaultValue = 23; | |
}); | |
it('it can also be a function', function() { | |
let defaultValue = () => {}; | |
function fn(value = defaultValue()) { | |
return value; | |
} | |
assert.equal(fn(), defaultValue()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment