简体   繁体   English

为什么原型允许多个实例共享变量?

[英]why prototype allow multiple instances share variables?

Say, 说,

function Person(name) {
    this.name = name;
}

Person.prototype.share = [];

Person.prototype.printName = function() {
    alert(this.name);
}

var person1 = new Person('Byron');
var person2 = new Person('Frank');

person1.share.push(1);
person2.share.push(2);
console.log(person2.share); //[1,2]

In the above, share can be used by all instances. 在上面,共享可以被所有实例使用。 I am wondering if this is specific to Javascript? 我想知道这是否专门针对Javascript吗? anything similar for other OOP language like C++, C# or Java? 与其他OOP语言(例如C ++,C#或Java)相似吗?

Because that's how prototypes work. 因为这就是原型的工作方式。

In classical OOP (Java, C# etc.), classes are just "templates", and inheritance is just combining the "templates" for instances to be created from. 在经典的OOP(Java,C#等)中,类只是“模板”,而继承只是将“模板”组合起来以创建实例。 In prototypal inheritance, an instance is an object whose parent is a live object . 在原型继承中,实例是对象,其父对象是活动对象 They don't just share definition, they share the same live instance of a parent. 它们不仅共享定义,而且共享父对象的相同实时实例。

So why does it work? 那为什么行得通呢? That's because of how prototype lookups work. 那是因为原型查找是如何工作的。 If some property isn't found in the instance, the engine looks for it in the parent. 如果在实例中找不到某些属性,则引擎会在父级中查找它。 If still not there, it looks higher up in the prototype chain until it reaches the root object. 如果还不存在,它将在原型链中向上看,直到到达根对象为止。 If still not there, the engine can declare it undefined (if it's a property lookup) or throw an error (if you called a method). 如果仍然不存在,则引擎可以将其声明为undefined (如果是属性查找)或引发错误(如果您调用了方法)。

instance -> parent -> parent -> parent -> Object

That's how JS "inherits". 这就是JS“继承”的方式。 It basically just looks for the ancestor that has that something you want, and operate at that level. 基本上,它只是寻找具有您想要的东西并在该级别上运行的祖先。 This is why instances of Person can push to share , because they have a common parent object. 这就是Person实例可以推送share原因,因为它们有一个公共的父对象。

person1
        \
         > Person.prototype (which has share[])
        /
person2

In order to prevent such sharing, one can override it by declaring a share property in the constructor (which when instantiated, creates one per instance). 为了防止这种共享,可以通过在构造函数中声明一个share属性来覆盖它(实例化时,每个实例创建一个)。 Another is just putting a share property on the instance. 另一个只是在实例上放置一个share属性。 Descendant properties take precedence over ancestor properties, since lookup starts from the bottom. 由于查找从底部开始,因此后代属性优先于祖先属性。

function Person(name) {
    this.name = name;
    this.share = []; // Instances will have share, but not shared anymore
}

// or 
person1.share = []; // Creates a share for person1 only

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模块/原型和多个实例 - Module/prototype and multiple instances 如何使用具有多个子原型对象的原型嵌套对象和共享变量? - How do nest objects and share variables using prototype having multiple sub-prototype objects? 为什么一个组件的多个实例共享同一状态? - Why do multiple instances of a component share the same state? jQuery插件允许多个实例 - Jquery plugin allow multiple instances 共享原型方法的实例,用于私有实例变量 - Instances sharing prototype methods for use with private instance variables 为什么在JavaScript中为实例变量声明属性 - Why declare properties on the prototype for instance variables in JavaScript Javascript Module Pattern使用Prototype创建多个实例 - Javascript Module Pattern create multiple instances using Prototype 防止构造函数的多个实例共享相同的原型属性 - Preventing multiple instances of a constructor function from sharing the same prototype properties 为什么数字在从Object.prototype继承时不是Object的实例? - Why numbers are not instances of Object while they inherit from Object.prototype? 如何在多个原型函数之间正确共享变量? - How to correctly share a variable between multiple prototype functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM