JavaScript 函数参数

从ECMAScript 6开始,有两个新的类型的参数:默认参数,剩余参数。

默认参数

在JavaScript中,函数参数的默认值是undefined

在过去,用于设定默认的一般策略是在函数的主体测试参数值是否为undefined,如果是则赋予一个值。

1
2
3
4
5
6
7
function multiply(a, b) {
b = (typeof b !== 'undefined') ? b : 1;

return a*b;
}

multiply(5); // 5

使用默认参数,则函数体的检查就不再需要了。

1
2
3
4
5
function multiply(a, b = 1) {
return a*b;
}

multiply(5); // 5

函数默认参数允许在没有值或undefined被传入时使用默认形参。

1
2
3
4
5
6
7
8
9
10
function test(num = 1) {
console.log(typeof num);
}

test(); // 'number' (num is set to 1)
test(undefined); // 'number' (num is set to 1 too)

// test with other falsy values:
test(''); // 'string' (num is set to '')
test(null); // 'object' (num is set to null)

已经遇到的参数可用于以后的默认参数。

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
function go() {
return ':P';
}

function withDefaults(a, b = 5, c = b, d = go(), e = this,
f = arguments, g = this.value) {
return [a, b, c, d, e, f, g];
}
function withoutDefaults(a, b, c, d, e, f, g) {
switch (arguments.length) {
case 0:
a;
case 1:
b = 5;
case 2:
c = b;
case 3:
d = go();
case 4:
e = this;
case 5:
f = arguments;
case 6:
g = this.value;
default:
}
return [a, b, c, d, e, f, g];
}

withDefaults.call({value: '=^_^='});
// node环境下输出
// [undefined, 5, 5, ":P", {value:"=^_^="}, {}, "=^_^="]
// 浏览器环境下输出
// [{value:"=^_^="}, 5, 5, ":P", Window, Arguments(1), undefined]

withoutDefaults.call({value: '=^_^='});
// [undefined, 5, 5, ":P", {value:"=^_^="}, {}, "=^_^="]

在函数体内的函数声明不能引用内部的默认参数,默认参数总是会被首先执行,而在函数体内部的函数声明会在之后生效。

1
2
3
4
// Doesn't work! Throws ReferenceError.
function f(a = go()) {
function go() { return ':P'; }
}

位于默认参数之后非默认参数。参数仍然设置为从左到右,覆盖默认参数,即使后面的参数没有默认值。

1
2
3
4
5
6
function f(x = 1, y) { 
return [x, y];
}

f(); // [1, undefined]
f(2); // [2, undefined]

可以通过解构赋值为参数赋值:

1
2
3
4
5
function f([x, y] = [1, 2], {z: z} = {z: 3}) { 
return x + y + z;
}

f(); // 6

剩余参数

1
2
3
4
5
6
7
8
9
10
11
function sum(...theArgs) {
return theArgs.reduce((previous, current) => {
return previous + current;
});
}

console.log(sum(1, 2, 3));
// expected output: 6

console.log(sum(1, 2, 3, 4));
// expected output: 10

如果函数的最后一个命名参数以…为前缀,则它将成为一个数组,其中从0(包括)到theArgs.length(排除)的元素由传递给函数的实际参数提供。

1
2
3
function(a, b, ...theArgs) {
// ...
}

在上面的例子中,theArgs将收集该函数的第三个参数(因为第一个参数被映射到a,而第二个参数映射到b)和所有后续参数。

剩余参数和 arguments对象的区别

剩余参数和 arguments 对象之间的区别主要有三个:

  • 剩余参数只包含那些没有对应形参的实参,而 arguments 对象包含了传给函数的所有实参。
  • arguments 对象不是一个真正的数组,而剩余参数是真正的 Array 实例,也就是说你能够在它上面直接使用所有的数组方法
  • arguments 对象还有一些附加的属性 (如 callee 属性)。

引入了剩余参数来减少由参数引起的样板代码。

1
2
3
4
5
6
7
8
9
10
11
12
// Before rest parameters, the following could be found:
function f(a, b) {
var args = Array.prototype.slice.call(arguments, f.length);

// …
}

// to be equivalent of

function f(a, b, ...args) {

}

剩余参数可以被解构,这意味着他们的数据可以被解包到不同的变量中。

1
2
3
4
5
6
7
function f(...[a, b, c]) {
return a + b + c;
}

f(1) // NaN (b and c are undefined)
f(1, 2, 3) // 6
f(1, 2, 3, 4) // 6 (the fourth parameter is not destructured)

示例

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
// 因为theArgs是个数组,所以你可以使用length属性得到剩余参数的个数:
function fun1(...theArgs) {
alert(theArgs.length);
}

fun1(); // 弹出 "0", 因为theArgs没有元素
fun1(5); // 弹出 "1", 因为theArgs只有一个元素
fun1(5, 6, 7); // 弹出 "3", 因为theArgs有三个元素

// 剩余参数包含了从第二个到最后的所有实参,然后用第一个实参依次乘以它们:
function multiply(multiplier, ...theArgs) {
return theArgs.map(function (element) {
return multiplier * element;
});
}

var arr = multiply(2, 1, 2, 3);
console.log(arr); // [2, 4, 6]

// 可以在剩余参数上使用任意的数组方法,而arguments对象不可以:
function sortRestArgs(...theArgs) {
var sortedArgs = theArgs.sort();
return sortedArgs;
}

alert(sortRestArgs(5,3,7,1)); // 弹出 1,3,5,7

function sortArguments() {
var sortedArgs = arguments.sort();
return sortedArgs; // 不会执行到这里
}

alert(sortArguments(5,3,7,1)); // 抛出TypeError异常:arguments.sort is not a function

// 为了在arguments对象上使用Array方法,它必须首先被转换为一个真正的数组。
function sortArguments() {
var args = Array.prototype.slice.call(arguments);
var sortedArgs = args.sort();
return sortedArgs;
}
console.log(sortArguments(5, 3, 7, 1)); // shows 1, 3, 5, 7

-------------本文结束感谢您的阅读-------------
0%