用 ... 把数组“炸开”成一个一个的值,用于合并、克隆、传参。
| 以前 · concat | 现在 · 扩展运算符 |
|---|---|
let a = ["王太利", "肖央"]; let b = ["曾毅", "玲花"]; let c = a.concat(b); console.log(c); // ["王太利","肖央","曾毅","玲花"] |
let a = ["王太利", "肖央"]; let b = ["曾毅", "玲花"]; let c = [...a, ...b]; console.log(c); // ["王太利","肖央","曾毅","玲花"] |
| 以前 · slice | 现在 · 扩展运算符 |
|---|---|
let arr = ["E", "G", "M"]; let copy = arr.slice(); console.log(copy); // ["E","G","M"] |
let arr = ["E", "G", "M"]; let copy = [...arr]; console.log(copy); // ["E","G","M"] |
| 以前 | 现在 |
|---|---|
let nums = [3, 9, 5, 1]; console.log(Math.max( nums[0], nums[1], nums[2], nums[3] )); // 9 |
let nums = [3, 9, 5, 1]; console.log(Math.max(...nums)); // 9(数组展开成一个一个参数) |
| 以前 | 现在 |
|---|---|
let divs = document.querySelectorAll("div");
let arr = Array.prototype.slice.call(divs);
|
let divs = document.querySelectorAll("div");
let arr = [...divs];
// 一行转成真数组,可用数组方法
|