<script>
function sum(x, y, z) {
let total = 0;
if (x) total += x;
if (y) total += y;
if (z) total += z;
console.log(`total:${total}`); // 注意(``)中的`是tab键上方的按键按下产生的,不是单引号
}
sum(5, "", 9); // total:14
function sum2(...m) {
扫描二维码关注公众号,回复: 11115308 查看本文章let total = 0;
for (var i of m) {
total += i;
}
console.log(`total:${total}`)
}
sum2(4, 8, 9, 10); // total:31
let sum3 = (...m) => {
let total = 0;
for (var i of m) {
total += i;
}
console.log(`total:${total}`);
}
sum3(4, 8, 9, 10); // total:31
var [x,y] = [4,8] // 数组的解构赋值
console.log(...[4, 8]); // ...arr称为数组的扩展,会打印出 4 8
let arr1 = [1, 3];
let arr2 = [4, 8];
console.log("concat: " + arr1.concat(arr2)); // concat: 1,3,4,8
console.log([...arr1, ...arr2]); // [1, 3, 4, 8]
var [x,y] = [4,8];
var [x, ...y] = [4, 8, 10, 30]; // 加3个点后,此时y是一种动态类型,此时x对应4,y对应8,10,30。...Arr称为数组的扩展
let [a, b, c] = 'es6'; // a:"e" b:"s"c:"6",分别一一对应,称为字符串的解构
let xy = [...'es6']; // ["e", "s", "6"] 称为字符串的扩展
</script>