Json 实在网页中传输数据的一种格式,无论是在数据传输,还是接口调用中都扮演着非常重要的存在
然而在日常使用中,却经常要规定 json 的格式。于是在 ES6 中,出现了 Object.assign()
这个方法
语法:
Object.assign(target, ...sources)
参数:
target
目标对象。
sources
源对象。
返回值:
目标对象。
一、
Object.assign()
方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象。它将返回目标对象。
例如:
const target = { a: 1 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
Object.assign 方法只会拷贝源对象自身的并且可枚举的属性到目标对象
二、
如果目标对象中的属性具有相同的键,则属性将被源对象中的属性覆盖。后面的源对象的属性将类似地覆盖前面的源对象的属性。
例如:
const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };
const returnedTarget = Object.assign(target, source);
console.log(target);
// expected output: Object { a: 1, b: 4, c: 5 }
console.log(returnedTarget);
// expected output: Object { a: 1, b: 4, c: 5 }
Object.assign 不会跳过那些值为 null 或 undefined 的源对象。也就是说值不能为 null 或 undefined
三、
Object.assign()
这个方法不支持 IE 浏览器和 Android webview
Comments NOTHING