我想弄清楚为什么当其中一个元素是HTML元素时,我无法合并2个或更多对象。将HTML元素对象与自定义对象合并
编辑:
为什么当我与像obj1
和obj2
我从公共属性OBJ 2得到的值,但Object.assign
2“正常”的对象合并时,我做同样的root
这是一个HTML元素我不“T获得其值(例如id
属性的值)
const root = document.getElementById("root");
const obj1 = { id: "obj1", textContent: "i am obj1" };
const obj2 = { id: "obj2", textContent: "i am obj2"};
const merged1 = Object.assign({}, obj1, obj2); // the new object got the id and textContent from obj2
const merged2 = Object.assign({}, obj1, root); // the new object got the id and textContent from obj1. why not from root?
console.log(merged1);
console.log(merged2);
console.log(root.id);
console.log(root.textContent);
<div id="root">i am root</div>
你期待' “我是OBJ1”'和' “我是obj2的”'要连接? – guest271314
nope,我希望得到一个新的对象,而'id'和'textContent'属性将从'root'对象获取它们的值作为它传递给'Object.assign'的最后一个参数。 –