我将深入探讨javascript对象操作问题。我的问题是:使用
在上面的代码中,我既包含了直接赋值操作
const me = Object.create(person);
和const me = person;
的区别在哪里?这两个操作都给出了类似的输出,即它们将对象引用到新变量me
中。
const person = {
isHuman: false,
printIntroduction: function() {
console.log(`My name is ${this.name}. Am I human? ${this.isHuman}`);
}
};
const me = Object.create(person);
me.name = 'Matthew';
me.isHuman = true;
me.printIntroduction();
const me2 = person;
me.name = 'Manan';
me.isHuman = false;
me.printIntroduction();
assignment
,也使用了Object.create()
进行赋值。这两个变量都指向对象“person”,但它们之间有什么区别?可以有人解释一下吗?
可能以前已经有人问过这个问题,但我找不到合适的解答。希望能给出简单易懂的解释 :-)。
me2 = {}
,然后将person分配给它或使用new关键字me2 = new Person()
。 - Ahmed Wahba