我遇到了 Douglas Crockfords Object.create 方法的一个特点,我希望有人能解释一下:
如果我创建一个对象——比如“person”——使用对象文字表示法,然后使用 Object.create 创建一个新对象——比如“anotherPerson”——它继承了初始“person”对象的方法和属性。
如果我然后更改第二个对象的名称值 - 'anotherPerson' - 它也会更改初始'person' 对象的名称值。
这仅在属性嵌套时发生,这段代码应该让您了解我的意思:
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
};
// initiate new 'person' object
var person = {
name: {
first: 'Ricky',
last: 'Gervais'
},
talk: function() {
console.log('my name is ' + this.name.first + ' ' + this.name.last);
}
}
// create anotherPerson from person.prototype
var anotherPerson = Object.create(person);
// change name of anotherPerson
anotherPerson.name.first = 'Stephen';
anotherPerson.name.last = 'Merchant';
// call talk method of both 'person' and 'anotherPerson' objects
person.talk(); // oddly enough, prints 'Stephen Merchant'
anotherPerson.talk(); // prints 'Stephen Merchant'
如果我要在不嵌套的情况下存储名称值,那么这种奇怪的行为就不会发生——例如
// initiate new 'person' object
var person = {
firstName: 'Ricky',
lastName: 'Gervais',
talk: function() {
console.log('my name is ' + this.firstName + ' ' + this.lastName);
}
}
// create anotherPerson from person.prototype
var anotherPerson = Object.create(person);
// change name of anotherPerson
anotherPerson.firstName = 'Stephen';
anotherPerson.lastName = 'Merchant';
// call talk method of both 'person' and 'anotherPerson' objects
person.talk(); // prints 'Ricky Gervais'
anotherPerson.talk(); // prints 'Stephen Merchant'
当使用带有构造函数和“new”关键字的经典继承风格时,似乎不会出现这种嵌套问题。
如果有人能够解释为什么会发生这种情况,我将不胜感激!?