ES6参数默认值
2.es6中函数默认值
function sum (a = 2, b = 3) {
return a + b;
}
console.log(sum(1, 2)); // 3
console.log(sum(4)); // 7
console.log(sum()); // 5
// 当需要第一个参数使用默认值,而指定第二个参数时,需要显式指定第一个参数值为undefined
// 需要注意,当显式指定参数为null时,不会使用默认值
console.log(undefined, 4); // 6
原创
2021-11-11 14:19:35 ·
922 阅读 ·
0 评论