⭐️ Object.assign(target, source1, source2, ...)

⭐️ 特点

典型案例

const test1 = { a: 1, b: 2 };
const test2 = { b: 3, c: 4 };
const test3 = { c: 5, d: 6 };

const test4 = Object.assign(test1, test2, test3);

console.log(test1);
console.log(test2);
console.log(test3);
console.log(test4);

test4.a = 233;
console.log(test1, test4); // 修改 test4,test1也变化

Untitled

test3.__proto__.e = 233;
// 其实就相当于在 Object.prototype 上挂载了 e
// 所以 test1、test2、test4 的原型上都会有 e
const v1 = 123;
const v2 = '123';
const v3 = true;
const v4 = function test() {};

const v5 = Object.assign({}, v1, v2, v3, v4);

console.log(v5);

Untitled

关键点:

const r1 = new Number(123);
const r2 = new String('123');
const r3 = new Boolean(true);
const r4 = new Function(function test() {});

console.log({
	r1, r2, r3, r4
});

[r1, r2, r3, r4].forEach(item => {
	for (var key in item) {
    console.log(key, item[key]);
  }
});