简体   繁体   English

将属性添加到实例或添加到原型属性有什么区别?

[英]What are the difference between adding a property to instance or adding to prototypeproperty?

I have the following class:我有以下 class:

      class PersonCl {
      constructor(fullName, age) {
        this.fullName = fullName;
        this.age= age;
      }
    
      // Instance methods
      // Methods will be added to .prototype property
      calcAge() {
        console.log(2037 - this.birthYear);
      }
    
      greet() {
        console.log(`Hey ${this.fullName}`);
      }
}

now i have lets say two object that were created from the constructor:现在我可以说两个从构造函数创建的 object:

const Joe = new PersonCl('Joe',23);
const Larisa = new PersonCl('Larisa',41);

i'm asking what is the difference between:我在问有什么区别:

1.adding the property species into constructor and set it exactly where we set the fullName and age. 1.将属性种类添加到构造函数中,并将其设置在我们设置全名和年龄的位置。

  1. or adding the following line outside the class declaration:或在 class 声明之外添加以下行:

    PersonCl.prototype.species = "Male"; PersonCl.prototype.species = "男性";

The "species" property placed on the prototype object will be visible on all instances in most (but not all situations).大多数情况下(但不是所有情况),原型 object 上的“物种”属性将在所有实例上可见。 As soon as your code makes a change to "species":一旦您的代码对“物种”进行了更改:

someInstance.species = "tiger";

then that instance will have a local property "species", and the value of "species" on other instances will not change.那么该实例将具有本地属性“species”,并且其他实例上的“species”值不会改变。

Clearly, when the property is directly added to instances in the constructor, all instances will be similarly initialized (given your sample code) but thereafter changes can still be made on an instance-by-instance basis.显然,当属性直接添加到构造函数中的实例时,所有实例都将被类似地初始化(给定您的示例代码),但此后仍然可以逐个实例进行更改。 The key point is that updating a property value on an object always makes a local, "own" property of that object and does not affect the prototype.关键是更新 object 上的属性值总是会生成该 object 的本地“自己”属性,并且不会影响原型。

Some methods intended to help work with object properties as a collection, like Object.keys() , only involve "own" properties, which means properties directly associated with an object and not inherited properties.一些旨在帮助将 object 属性作为集合使用的方法,例如Object.keys()涉及“自己的”属性,这意味着与 object 直接关联的属性而不是继承的属性。

  1. Prototype is a real object原型是真正的object
  2. A lot of objects could have the same prototype很多对象可以有相同的原型
  3. Prototype could have prototype too原型也可以有原型
  4. When you read object property JS will look for own property first, and if it does not exists, it will look for this property in prototype and in prototype of prototype, etc.阅读 object 属性时,JS 会先查找自己的属性,如果不存在,会在原型和原型的原型等中查找该属性。

暂无
暂无

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

相关问题 React 组件实例属性和状态属性有什么区别? - What is the difference between React component instance property and state property? CSS中添加.classA和.classB.classA有什么区别? - What is the difference between adding .classA and .classB.classA in CSS? 向Javascript原型添加方法与向现有对象添加功能之间有什么区别? - What is the difference between Adding Methods to Javascript Prototype and Adding Functionality to Existing Objects 直接向 JavaScript 构造函数添加方法和使用原型添加方法有什么区别? - What is the difference between adding a method to a JavaScript constructor directly and adding a method using prototype? 向构造函数或原型添加属性之间的区别 - Difference between adding properties to constructor or prototype Promise.all和使用加号添加承诺之间的区别是什么? - What's the difference between Promise.all and adding promises using plus sign? Joi.object().keys() 和 append() 方法在添加更多键方面有什么区别 - What's the difference between Joi.object().keys() and append() methods in terms of adding more keys 添加.indexOn安全规则与不添加安全规则之间有性能差异吗? - Is there a performance difference between adding a .indexOn security rule and not adding one? 在此添加属性与在此添加属性。state。 有什么不同? - Adding properties on this vs adding properties on this.state. What's the difference? 将Java脚本库添加为npm依赖项或仅将它们包含在HTML中有什么区别? - What's the difference between adding Java Script libraries as npm dependencies or simply including them in HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM