简体   繁体   English

构造函数属性:__ proto __。constructor vs prototype.constructor

[英]Constructor property : __proto__.constructor vs prototype.constructor

I am fairly new to Javascript and I am trying to dig deep and understand inheritance, constructor functions and prototype chain. 我是Javascript的新手,我正在深入挖掘并理解继承,构造函数和原型链。 So, I created a constructor function as such, 所以,我创建了一个构造函数,

var a = function(){this.integer=1,this.float=1.0,this.string="one"}

Now, this function has a prototype.constructor property, a constructor property and a __proto__.constructor property. 现在,这个函数有一个prototype.constructor属性,一个构造函数属性和一个__proto__.constructor属性。

I understand that the __proto__.constructor == the constructor function executed to create the function a. 我理解__proto__.constructor ==执行的构造函数创建函数a。 Also, the prototype.constructor is the function that is executed when I create an instance of 'a' using the new keyword. 此外,prototype.constructor是我使用new关键字创建'a'实例时执行的函数。

However I don't understand what the third constructor property is for. 但是我不明白第三个构造函数属性是什么。 Its equal to __proto__.constructor . 它等于__proto__.constructor

Also, b.__proto__.constructor !== Object.prototype.constructor , as I thought it would be. 另外, b.__proto__.constructor !== Object.prototype.constructor ,正如我想的那样。 Why is that ? 这是为什么 ?

a.__proto__ is the Function prototype (namely, Function.prototype ). a.__proto__Function原型(即Function.prototype )。 It is the object from which all functions inherit function-specific methods like call , apply , bind , etc. It is true that a.__proto__.bind == a.bind . 它是所有函数继承特定于函数的方法(如callapplybind等)的对象。确实a.__proto__.bind == a.bind

a.__proto__.constructor is the Function constructor, ie, the function Function . a.__proto__.constructorFunction构造函数,即函数Function The Function prototype has a reference to its associated constructor via the constructor property, as is always the default relationship between prototype object and constructor. Function原型通过constructor属性引用其关联的构造constructor ,因为它始终是原型对象和构造函数之间的默认关系。 (More on this "default relationship" in the next two paragraphs.) (更多关于这两段中的“默认关系”。)

Quite different is a.prototype -- in JavaScript, any function can be a constructor, ie, it may be called with new . 完全不同的是a.prototype - 在JavaScript中,任何函数都可以是构造函数,也就是说,它可以用new调用。 Whenever a function is called with new , it creates a new object whose __proto__ is the function's prototype and points to the newly-created object via this . 每当使用new调用函数时,它都会创建一个新对象,其__proto__是函数的prototype并通过this指向新创建的对象。 So inside a call to new a() it is true that this.__proto__ equals a.prototype . 所以在调用new a()时, this.__proto__等于a.prototype This prototype object is automatically created and stored in a.prototype at the moment the function a is defined. 在定义函数a时,自动创建该原型对象并将其存储在a.prototype中。

a.prototype.constructor is equal to a , because the JavaScript internal routine that creates prototype objects for newly-defined functions (as specified in the previous paragraph) always gives that new prototype a constructor property that refers to the newly-defined function. a.prototype.constructor等于a ,因为为新定义的函数创建prototype对象的JavaScript内部例程(如前一段所述)总是为新原型提供一个constructor属性,该属性引用新定义的函数。 To get really into the weeds, the relevant ECMAScript routine is 19.2.1.1.1, CreateDynamicFunction , which notes, "A prototype property is automatically created for every function created using CreateDynamicFunction, to provide for the possibility that the function will be used as a constructor." 为了真正进入杂草,相关的ECMAScript例程是19.2.1.1.1,CreateDynamicFunction ,它注意到“为使用CreateDynamicFunction创建的每个函数自动创建一个prototype属性,以提供将该函数用作构造函数“。

a does not have its own constructor property, but it automatically inherits a.__proto__.constructor accessible as a.constructor , just as it inherits any other properties on its prototype parent (just like a.bind is really a.__proto__.bind ). a没有自己的constructor属性,但它会自动继承a.__proto__.constructor可以作为a.constructor访问,就像它继承其原型父级的任何其他属性一样(就像a.bind实际上a.__proto__.bind )。

Finally, a.__proto__.constructor !== Object.prototype.constructor because Object.prototype is not a function object's prototypal parent, but instead Function.prototype is. 最后, a.__proto__.constructor !== Object.prototype.constructor因为Object.prototype不是一个函数对象的原型父类,而是Function.prototype It is instead true that a.__proto__.constructor === Function.prototype.constructor (and, more succinctly, a.__proto__ == Function.prototype and a.__proto__.constructor == Function ). 相反,它确实a.__proto__.constructor === Function.prototype.constructor (更简洁地说, a.__proto__ == Function.prototypea.__proto__.constructor == Function )。

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

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