美文网首页
ES6 展开运算符

ES6 展开运算符

作者: 黄黄黄大帅 | 来源:发表于2020-08-06 09:25 被阅读0次
合并数组
var arr1 = ['two', 'three'];
var arr2 = ['one', ...arr1, 'four', 'five'];

// ["one", "two", "three", "four", "five"]
拷贝数组
var arr = [1,2,3];
var arr2 = [...arr]; // 和arr.slice()差不多
arr2.push(4)
解构赋值
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }
将数组“展开”成为不同的参数
let numbers = [9, 4, 7, 1];
Math.min(...numbers); // 1
将类数组转换成数组
  • arguments、NodeList、classList等
[...document.querySelectorAll('div')]
var myFn = function(...args) {
// ...
}

注:Array.from() 、Array.prototype.slice.call(obj) 、Array.apply(null, obj) 也可以

相关文章

网友评论

      本文标题:ES6 展开运算符

      本文链接:https://www.haomeiwen.com/subject/nsyorktx.html