🌈 对 length 的影响

只会计算给默认值的参数前面的个数

function test(a, b, c) {}
test(1);
console.log(test.length); // 3
function test(a, b = 1, c) {}
test(1);
console.log(test.length); // 1
function test(a = 1, b, c) {}
test(1);
console.log(test.length); // 0

对 arguments 的影响

function test(a, b, c, d, e, f) {
	console.log(arguments);
}
test(1, 2, 3, 4, 5, 6);

Untitled

function test(a, b, c, d, e, f) {
  // b = 7;
  arguments[1] = 7;
	console.log(arguments);
}
test(1,2,3,4,5,6);

Untitled

参数中给了默认值之后,arugments 就和实参无法一一对应了

function test(a, b, c = 1, d, e, f) {
  arguments[1] = 7;
  console.log(arguments);
	console.log(b);
}
test(1, 2, 3, 4, 5, 6);
// b还是2

Untitled

一般给解构默认对象再添加一个默认空对象,这样用户不传也不会报错了

function foo({ x, y = 5 } = {}) {
	console.log(x, y);
}
foo({}); // undefined 5
foo({x: 1}); // 1 5
foo({x: 1, y: 2}); // 1 2
foo(); // undefined 5