zoukankan      html  css  js  c++  java
  • Object.assign() 对象的扩展

    object.assign()方法用于对象的合并,将源对象的(source)的所有的可枚举属性,复制到目标对象(target)

    var target = {a:1};
    
    var source1={b:2};
    var source2={c:3};
    
    Object.assign(target,source1,source2);
    target //{a:1,b:2,c:3}

    Object.assign方法的第一个参数是目标对象。后面的参数是元对象。

    注意,如果目标对象与源对象有同名的属性,或多个源对象有同名属性,则后面的属性会覆盖前面的属性。

    var target = { a: 1, b: 1 };
    
    var source1 = { b: 2, c: 2 };
    var source2 = { c: 3 };
    
    Object.assign(target, source1, source2);
    target // {a:1, b:2, c:3}
    

      如果只有一个参数,Object.assign会直接返回该参数。

    1 var obj={a:1}
    2 Object.assign(obj)===obj //true
  • 相关阅读:
    CCF201604试题
    CCF201512试题
    CCF201509试题
    CCF201509试题
    CCF201503试题
    CCF201503试题
    CCF201412试题
    CCF201412试题
    CCF201409试题
    CCF201409试题
  • 原文地址:https://www.cnblogs.com/wy1935/p/7111541.html
Copyright © 2011-2022 走看看