美文网首页
展开语法(Spread syntax)

展开语法(Spread syntax)

作者: SingleDiego | 来源:发表于2019-03-05 15:15 被阅读0次

    参考文档:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax

    合并数组:

    const first = [1, 2, 3];
    const second = [4, 5, 6];
    
    [...first, ...second]; // [1, 2, 3, 4, 5, 6]
    

    合并数组的同时插入元素:

    ['a', ...first, ...second]; // ["a", 1, 2, 3, 4, 5, 6]
    
    ['a', ...first, 'b', ...second, 'c']; // ["a", 1, 2, 3, "b", 4, 5, 6, "c"]
    

    复制数组:

    [...first]; // [1, 2, 3]
    

    合并对象:

    const first = {name: 'tom'};
    const second = {age: 18};
    
    {...first, ...second}; // {name: "tom", age: 18}
    

    合并对象并插入新属性:

    {...first, ...second, location: 'china'}; // {name: "tom", age: 18, location: "china"}
    

    复制对象:

    {...first}; // {name: "tom"}
    

    相关文章

      网友评论

          本文标题:展开语法(Spread syntax)

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