函数扩展
function fn(x) { x = x || 'hello' }
function fn(x = 'hello') {}
function foo({x = 2, y = 5}) { console.log(x, y) }
foo({x: 1})
foo()
function foo({x = 2, y = 5} = {}) { console.log(x, y) }
foo()
function f(x = 1, y) { return [x, y] }
f(, 1)
f(undefined, 1)
(function (a) {}).length
(function (a, b, c = 5) {}).length
(function (a, b = 1, c, d) {}).length
let x = 1
function f(y = x) {
let x = 2
console.log(y)
}
f()
var x = 1
function foo(x, y = function() { x = 2 }) {
var x = 3
y()
console.log(x)
}
foo()
x
var x = 1
function foo(x, y = function() { x = 2 }) {
x = 3
y()
console.log(x)
}
foo()
x
function throwIfMissing() {
throw new Error('Missing parameter')
}
function foo(param = throwIfMissing()) {
return param
}
foo()
function foo(param = undefined) {}
function sortNumbers() { return Array.prototype.slice.call(arguments).sort() }
const sortNumbers = (...numbers) => numbers.sort()
(function(...args) {}).length
(function(a, ...args) {}).length
function doSomething(a, b = a) {
'use strict'
}
const doSomething = function({ a, b }) {
'use strict'
}
const doSomething = (...rest) => {
'use strict'
}
'use strict'
const doSomething = (x, y = x) => {
console.log(x, y)
}
doSomething(1)
const doSomething = (function() {
'use strict'
return function(value = 42) {
return value;
}
}())
const doSomething = function() {
'use strict'
console.log(Array.from(arguments))
}
doSomething(1, 2, 3)
var f = function() {}
const bar = function baz() {}
bar.bind(null).name
(new Function).name
(function() {}).name
(function() {}).bind(null).name
let f = v => v
let f = function(v) {
return v
}
let f = () => 5
let sum = (num1, num2) => num1 + num2
let sum = (num1, num2) => {
return num1 + num2
}
let getItem = id => ({ id: id, name: 'Test' })
# 1.函数体内的this对象就是定义时所在的对象, 而不是使用时所在的对象, this对象的指向是可变的, 但在箭头函数中它是固定。箭头函数根本没有自己的this, 内部的this其实是外层代码块的this
# 2.不可以当作构造函数。即, 不可以使用new命令, 否则抛出错误
# 3.不可以使用arguments对象, 该对象在函数体内不存在。可以使用rest参数代替
# 4.不可以使用yield命令, 即箭头函数不能用作Generator函数
function Timer() {
this.s1 = 0, this.s2 = 0
setInterval(() => this.s1++, 1000)
setInterval(function () {
this.s2++
}, 1000)
}
let timer = new Timer()
setTimeout(() => console.log('s1: ', timer.s1), 3100)
setTimeout(() => console.log('s2: ', timer.s2), 3100)
function foo() {
setTimeout(() => {
console.log('id: ', this.id)
}, 100)
}
function foo() {
let _this = this
setTimeout(function() {
console.log('id: ', _this.id)
}, 100)
}
function foo() {
setTimeout(() => { console.log(Array.from(arguments)) }, 100)
}
foo(2, 4, 6, 8)
(function() {
return [
(() => this.x).bind({ x: 'inner' })()
]
}).call({ x: 'outer' })
const pipeline = (...funcs) => val => funcs.reduce((prev, curv) => curv(prev), val)
const plus = a => a + 1
const mult = a => a * 2
const addThenMult = pipeline(plus, mult)
addThenMult(5)
foo::bar
foo::bar(...arguments)
let method = ::obj.foo
let log = ::console.log
getPlayers()::map(x => x.character())::takeWhile(x => x.strength > 100)::forEach(x => console.log(x))
function f(x) {
return g(x)
}
function f(x) {
let y = g(x)
return y
}
function f(x) {
return g(x) + 1
}
function f(x) {
g(x)
}
function f(x) {
if (x > 0) {
return m(x)
}
return n(x)
}
function f() {
let m = 1
let n = 2
return g(m + n)
}
function f() {
return (3)
}
f()
g(3)
function addOne(a) {
let one = 1
function inner(b) {
return b + one
}
return inner(a)
}
'use strict'
function fibonacci(n) {
if (n <= 1) { return 1 }
return fibonacci(n - 1) + fibonacci(n - 2)
}
fibonacci(100000)
function fibonacci(n, ac1 = 1, ac2 = 1) {
if (n <= 1) { return ac2 }
return fibonacci(n - 1, ac2, ac1 + ac2)
}
fibonacci(100000)
function tco(f) {
let value;
let active = false;
var accumulated = [];
return function accumulator() {
accumulated.push(arguments);
if (!active) {
active = true;
while (accumulated.length) {
value = f.apply(this, accumulated.shift());
}
active = false;
return value;
}
}
}
const sum = tco(function(x, y) {
if (y > 0) {
return sum(x + 1, y - 1);
} else {
return x
}
});
sum(1, 100000)
function sum(x, y) {
if (y > 0) {
return sum.bind(null, x + 1, y - 1);
} else {
return x
}
}
function trampoline(f) {
while (f && f instanceof Function) {
f = f();
}
return f;
}
trampoline(sum(1, 100000))
function clownsEverywhere(
param1,
param2,
) {}
clownsEverywhere(
'foo',
'bar',
)