function foo(a, b){}
foo.length // 2
Functions
length property
name attribute
function foo() {}
foo.name // "foo"
Used in conjunction with destructuring assignment defaults
function foo({x, y = 5} = {}) {
console.log(x, y);
}
foo() // undefined 5
Parameter setting default value
function log(x, y = 'World') {
console.log(x, y);
}
log('Hello') // Hello World
log('Hello', 'China') // Hello China
log('Hello', '') // Hello
Arrow function
#Arrow functions
setTimeout(() => {
···
})
#with parameters
readFile('text.txt', (err, data) => {
...
})
#implicit return
arr.map(n => n*2)
//no curly braces = implicit return
//Same as: arr.map(function (n) { return n\*2 })
arr.map(n => ({
result: n*2
}))
//Implicitly returning an object requires parentheses around the object
Like a function, but preserves this
.
See: Arrow functions
Comments