es6函数扩展

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 函数扩展部分
{
//函数默认值
function test (x,y ='aaa') { // 默认值后面不能再有没有默认值的变量 (x, y = 'aa', c)不可以
console.log(x,y);
}
test('hello')
}
// hello aaa

{
let x = 'test'
function test2 (x ,y = x) {
console.log(x, y);
}
function test3 (c ,y = x) {
console.log(c, y);
}
test2('a')
test2()
test3('a')
}
// a a undefined undefined a test

{
// res参数 res参数之后不能有其他参数了
function test(...arg) {
for(let v of arg) {
console.log(v);
}
}
test(1,2,3,4, [1,2,3])
}
// 1 2 3 4 [1,2,3]

{
// 扩展运算符
console.log(...[1,2,4]);
console.log('a', ...[1,2,4]);
}
// 1 2 4 a 1 2 4

{
// 箭头函数 注意this绑定
let arrow = v => v*2
let arrow2 = () => 20
console.log(arrow(2));
console.log(arrow2());
}
// 4 20

{
// 尾调用 是用来提升性能的,当某个函数嵌套调用另外一个函数建议使用尾调用
function tail(x) {
console.log(x);
}
function fx(x) {
return tail(x)
}
fx(123)
}
// 123