Repository navigation
Expand file tree
/
Copy pathresidual-parameters.js
More file actions
101 lines (87 loc) · 2.44 KB
/
Copy pathresidual-parameters.js
File metadata and controls
101 lines (87 loc) · 2.44 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//++++++++++++++++++++++++++++++++++++++++++++++++
//函数参数的默认值
function powerOf(base,exponent = 2) {
return Math.pow(base,exponent)
}
console.log(powerOf(2));
console.log(powerOf(2, undefined));
console.log(powerOf(2, 3));
var double = (value = 0) => value * 2
console.log(double());
console.log(double(2));
//对象参数默认值
// var defaultOption = {
// brand: 'volvo',
// make: 2019
// }
// function carFactory(option = defaultOption) {
// console.log(option.brand);
// console.log(option.make);
// }
// carFactory()
// //volvo
// //2019
//
// //如果传入一个对象参数,则所有的默认值都会失效
// carFactory({})
// // undefined
// // undefined
//
// carFactory({make: 2010})
// // undefined
// // 2010
//与提供一个默认参数相比,更好的办法是使用解构,并在解构模式中为每一个属性指定默认值
function carFactory({brand = 'volvo',make = 2019}) {
console.log(brand);
console.log(make);
}
// carFactory({})
// // volvo
// // 2019
//
// carFactory({make: 2010})
// // volvo
// // 2010
//
// carFactory()
// //Exception
// 添加一个空对象作为默认值可避免该问题,因为解构空对象时已经提供了默认值
function carFactory({brand = 'volvo',make = 2019} = {}) {
console.log(brand);
console.log(make);
}
carFactory()
// volvo
// 2019
//只想在某个函数中提取对象的某些属性作为参数,可以通过解构提前显式地引用这些属性
var getCarProductModel = ({brand,make,model} = {})=>({
sku: brand + ':' + make + ':' + model,
brand,
make,
model
})
var car = {
brand: 'volvo',
make: 2019,
// model: '201',
price: 280000,
color: 'red'
}
console.log(getCarProductModel(car));
console.log(getCarProductModel({}));
console.log(getCarProductModel());
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//剩余参数
//在函数的最后一个参数前添加...可以将该参数转变为一个特殊的剩余参数。
//当剩余参数是函数中的唯一参数时,它会获取所有传入函数的参数
// function join(...list) {
// return list.join(',')
// }
// console.log(join('first', 'second', 'third'));
// var join = (...list) => list.join(',')
// console.log(join('first', 'second', 'third'));
//剩余参数前面的参数不会包含在 list 参数中
function join(separator,...list) {
return list.join(separator)
}
console.log(join(';', 'first', 'second', 'third'));