JS 扩展运算符

扩展运算符(…)

把数组对象转成参数序列(使用逗号分隔的序列)

['a', 'b', 'c'] => 'a', 'b', 'c'
{left: 100, top: 200} => left: 100, top: 200

求数组最大值:

var arr = [1, 7, 3, 6, 2];

console.log(Math.max(...arr));

结果为:

7

数组合并:

var arr1 = [1, 7, 3, 6, 2];
var arr2 = ['a', 'b', 'c'];
var arr3 = [
    ...arr1,
    ...arr2
]

console.log(arr3);

结果为:

[1, 7, 3, 6, 2, "a", "b", "c"]

对象合并:

let obj1 = { left: 100, top: 200 }
let obj2 = { width: 200, height: 200 };
let obj3 = {
    ...obj1,
    ...obj2
}

console.log(obj3)

结果为:

{left: 100, top: 200, width: 200, height: 200}

你可能感兴趣的:(Js)