【问题标题】:Prototype Concept in JavaScriptJavaScript 中的原型概念
【发布时间】:2019-12-16 12:29:20
【问题描述】:

我有一些关于原型的问题。

如果我们通过Person 函数构造函数创建了一个对象,比如说Dog。 假设 Person 在每个实例成员上有 2 个属性。

var Dog = function (name,color){
   this.name =  name,
   this.color = color
};

现在我们已经创建了 10 个对象,例如 Pug、Bulldog 等。

var Pug =new Dog('pug','white');
var Bulldog =new Dog('Bulldog','black');

现在我们突然想在 Dog 中添加一个属性,以便所有 object 都可以访问它。我们可以通过两种方式做到这一点:

  1. 在函数构造函数中添加它this.leg : 4
var Dog = function (name,color){
    this.name =  name,
    this.color = color,
    this.leg = 4
};

或

  1. 使用
  2. 添加它
Dog.prototype.leg = 4;

现在我的问题是在这些变体中的每一个中,属性将被添加其原型属性意味着作为原型成员而不是作为实例成员?为什么?

如果在创建对象后我们想要添加一些属性并且我们希望它作为实例成员添加意味着自己的属性我该怎么做?

我在浏览器控制台中得到了什么:

Pug { name : 'pug',color :'white'}
_ proto_ : { leg :4 }

我的期望:

Pug { name : 'pug',color :'white',leg :4 }
_proto_ :Object

【问题讨论】:

  • 在第一个选项中,您不会更改原型 - 如果您更改构造函数,那么您会得到预期的结果,因为这意味着通过构造函数创建的任何新实例都有财产。如果您想更改现有 成员,那么您别无选择自动执行此操作 - 您可以重新创建所有成员或收集所有成员并循环更改它们。您不能自动将属性从原型转移到每个实例。

标签: javascript prototype prototype-chain jsobject


【解决方案1】:

当您这样做Dog.prototype.leg = 4; 时,您实际上是在修改原型本身。但是,未来实例化的对象将继承该新属性作为实例属性。

内存中的现有实例在实例化时将继续使用预先存在的实例属性。

var Dog = function (name,color){
    this.name =  name,
    this.color = color
};

console.log("Existing instances in memory");
let toy = new Dog("toy", "brown");
let tay = new Dog("tay", "grey");
console.log(toy, "\n", tay); // Existing instances in memory.

console.log("Future instance in memory.");
Dog.prototype.leg = 4;
let toe = new Dog("tay", "black");
console.log(toe); // Future instance in memory.
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • "所以未来的对象也将继承那个新属性",任何当前的对象,因为原型链是活的对象。跨度>
  • @VLAZ 原型对象是的,实例(构造函数属性)没有。
  • 您的回答表明,在您修改原型后,当前对象 不会获得leg 属性,但它们会。 “当前”和“未来”对象之间没有区别 - 在这两种情况下,它们都会获得 leg 属性,并且它始终来自原型,它不是实际 Dog 对象上的实例属性。
  • 答案仍然不正确——对象没有得到实例属性——原型改变前后的所有Dog对象从原型得到新的原型。 Here is what happens with the old objects after changing the prototype。旧的和新的都不会获得新的实例属性——它总是同一个。他们都拥有相同的财产,如果你这样做toy.leg = 3,那么所有狗都是三足的。
  • 抱歉,toy.leg = 3 将分配一个实际的实例属性,因此其余的狗将拥有leg = 4。不过,原型属性不是实例属性。
【解决方案2】:

使用选项 1 将在每个创建的新对象上附加一个实例属性:

var Dog = function (name,color){
   this.name =  name,
   this.color = color
};

var Pug = new Dog('pug','white');
var Bulldog = new Dog('Bulldog','black');

//overwrite the constructor function
var Dog = function (name,color){
    this.name =  name,
    this.color = color,
    this.leg = 4
};

var Dalmatian = new Dog('dalmatian', 'spotted');

//check the browser console as well
console.log(Dalmatian);
console.log(Dalmatian.leg)
console.log(Dalmatian.hasOwnProperty('leg'));

console.log(Bulldog);
console.log(Bulldog.leg)
console.log(Bulldog.hasOwnProperty('leg'));

既然如此,这意味着您将获得您期望的准确输出。但是,这仅适用于创建的任何 new 对象实例——旧的对象实例不会被修改,如上所示。

使用选项 2所有 实例将通过其原型访问 leg 属性,无论您何时完成修改:

var Dog = function (name,color){
   this.name =  name,
   this.color = color
};

var Pug = new Dog('pug','white');
var Bulldog = new Dog('Bulldog','black');

//modify prototype
Dog.prototype.leg = 4;

var Dalmatian = new Dog('dalmatian', 'spotted');

//check the browser console as well
console.log(Dalmatian);
console.log(Dalmatian.leg)
console.log(Dalmatian.hasOwnProperty('leg'));

console.log(Bulldog);
console.log(Bulldog.leg)
console.log(Bulldog.hasOwnProperty('leg'));

因此,这将影响在更改之前创建的对象,但如果您希望这都是实例属性,那么这不会发生 - 正如您所看到的那样,.hasOwnProperty('leg') 返回false,因为它们全部 em> 通过原型访问它。

如果您想修改在某些更改之前和之后创建的所有 Dog 对象 - 原型链是最简单的选择。但是,如果您想修改 instances,则没有简单的方法可以做到。您可以重新创建所有对象:

var Dog = function (name,color){
   this.name =  name,
   this.color = color
};

var Pug = new Dog('pug','white');
var Bulldog = new Dog('Bulldog','black');

//overwrite the constructor function
var Dog = function (name,color){
    this.name =  name,
    this.color = color,
    this.leg = 4
};

var Dalmatian = new Dog('dalmatian', 'spotted');

//re-create
Pug = new Dog(Pug.name, Pug.color);
Bulldog = new Dog(Bulldog.name, Bulldog.color);

//check the browser console as well
console.log(Dalmatian);
console.log(Dalmatian.leg)
console.log(Dalmatian.hasOwnProperty('leg'));

console.log(Bulldog);
console.log(Bulldog.leg)
console.log(Bulldog.hasOwnProperty('leg'));

或者修改它们:

var Dog = function (name,color){
   this.name =  name,
   this.color = color
};

var Pug = new Dog('pug','white');
var Bulldog = new Dog('Bulldog','black');

//overwrite the constructor function
var Dog = function (name,color){
    this.name =  name,
    this.color = color,
    this.leg = 4
};

var Dalmatian = new Dog('dalmatian', 'spotted');

//modify
Pug.leg = 4;
Bulldog.leg = 4;

//check the browser console as well
console.log(Dalmatian);
console.log(Dalmatian.leg)
console.log(Dalmatian.hasOwnProperty('leg'));

console.log(Bulldog);
console.log(Bulldog.leg)
console.log(Bulldog.hasOwnProperty('leg'));

在任何一种情况下,您都需要引用所有旧对象。

选项 1 需要注意的最后一件事是,如果您更改函数,那么您将创建新的 Dog 对象,但它们不会是 相同的 @ 987654330@ 对象和以前一样,因为现在功能不同了:

var Dog = function (name,color){
   this.name =  name,
   this.color = color
};

var Pug = new Dog('pug','white');
var Bulldog = new Dog('Bulldog','black');

console.log("Bulldog is dog:", Bulldog instanceof Dog); //true - constructed with current Dog

//overwrite the constructor function - it's now *different*
var Dog = function (name,color){
    this.name =  name,
    this.color = color,
    this.leg = 4
};

var Dalmatian = new Dog('dalmatian', 'spotted');

console.log("Dalmatian is dog:", Dalmatian instanceof Dog); //true - constructed with  the new Dog

console.log("Bulldog is dog:", Bulldog instanceof Dog); //false - *not* constructed with the new Dog

如果这种差异很重要,那么您要么需要一种不同的方式来识别对象,要么重新创建它们。或者,原型更改将避免不匹配的构造函数,但同样,您不会获得实例属性。

【讨论】:

  • 非常感谢
猜你喜欢
  • 2014-08-20
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 2013-03-30
  • 2021-11-28
  • 2018-08-28
相关资源
最近更新 更多