简体   繁体   English

向函数,对象和原型添加属性

[英]Adding properties to function, object and prototype

Lets say I have this function: 可以说我有这个功能:

function myFunc () {
    this.property1 = 10;
}
var myObject = new myFunc();

Now lets say I want to add a new property to it. 现在说我要向其中添加一个新属性。

myFunc.newProperty = "New"; 
myObject.newProperty = "New";
myFunc.prototype.newProperty = "New";

What is difference between these aproaches? 这些方法有什么区别? And which one should I use? 我应该使用哪一个呢?

Approach 1 方法1

myFunc.newProperty = "New"; 

Because the function itself is an object, you just create a new property on it. 由于函数本身是一个对象,因此只需在其上创建一个新属性。 It may be useful you if you work directly with the Function object . 如果直接使用Function对象,这可能对您有用。

Approach 2 方法2

myObject.newProperty = "New";

On a new instance of the myFunc constructor, you create an own property . myFunc构造函数的新实例上, 创建一个自己的property Useful when you need to modify a single instance, but don't want to modify the class itself with newProperty . 当您需要修改单个实例,但又不想使用newProperty修改类本身时newProperty
Any new instances created with new myFunc() will not inherit or contain the newProperty . 使用new myFunc()创建的任何新实例都不会继承或包含newProperty

Approach 3 方法3

myFunc.prototype.newProperty = "New";

If you modify the constructor prototype , then the created objects will inherit this property. 如果修改构造函数原型 ,则创建的对象将继承此属性。 Useful when you need any existing or new object created with new myFunc() to inherit the newProperty . 当您需要使用new myFunc()创建的任何现有对象或新对象来继承newProperty ,此方法newProperty

Which one to use depends on the task. 使用哪个取决于任务。 The points 2 and 3 are commonly used. 23是常用的。

myFunct is Constructor function(in fact every function in JS can be used as Constructor), when used with new this function creates a new object. myFunct是构造函数(实际上,JS中的每个函数都可以用作构造函数),当与new一起使用时,此函数将创建一个新对象。

now you want to add new properties to object: 现在您想向对象添加新属性:

1) want to add new property to already created myObject 1)想要向已经创建的myObject添加新属性

myObject.newProperty = "New";

2) want to add new property to every object created from myFunc 2)想要向myFunc创建的每个对象添加新属性

function myFunc () {
    this.property1 = 10;
    this.newProperty = "New"; 
}

3) want to have new property in all the object already created from myFunc and it should be a property of parent object(so that only one copy is available) for all the newly created object from myFunc 3)希望在所有已经通过myFunc创建的对象中都具有新属性,并且对于所有从myFunc新创建的对象,它都应该是父对象的属性(以便只有一个副本可用)

myFunc.prototype.newProperty = "New";

So when you are doing 所以当你做的时候

myFunc.newProperty = "New"; 

this property is is treated as private Property of myFunc so even if you make some instance of myFunc you will not able to access. 此属性被视为myFunc的私有属性,因此,即使您创建了myFunc的某些实例,也将无法访问。

Same in the case of 情况相同

myObject.newProperty = "New";

when you are making it its treated a private property of myObject so outside of myObject you can not access it. 当您将其设置为myObject的私有属性时,在myObject之外无法访问它。

And when you are doing 当你做的时候

myFunc.prototype.newProperty = "New";

You can access newProperty from all the instance of myFunc. 您可以从myFunc的所有实例访问newProperty。

Which is good which is bad i cant say its all requirement 哪个好,哪个不好,我不能说所有要求

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM