构造函数 属性 :__proto__.constructor 与 prototype.constructor
Constructor property : __proto__.constructor vs prototype.constructor
我是 Javascript 的新手,我正在尝试深入挖掘并理解继承、构造函数和原型链。
所以,我创建了一个构造函数,
var a = function(){this.integer=1,this.float=1.0,this.string="one"}
现在,这个函数有一个 prototype.constructor 属性,一个构造函数 属性 和一个 __proto__.constructor
属性。
我的理解是__proto__.constructor
==执行的构造函数创建函数a。
此外,prototype.constructor 是我使用 new 关键字创建 'a' 实例时执行的函数。
但是我不明白第三个构造函数 属性 是干什么用的。它等于 __proto__.constructor
.
另外,b.__proto__.constructor
!== Object.prototype.constructor
,正如我所想的那样。这是为什么?
a.__proto__
是Function
原型(即Function.prototype
)。它是所有函数继承函数特定方法的对象,如 call
、apply
、bind
等。 a.__proto__.bind == a.bind
.
是真的
a.__proto__.constructor
是 Function
构造函数,即函数 Function
。 Function
原型通过 constructor
属性 引用其关联的构造函数,这始终是原型对象和构造函数之间的默认关系。 (在接下来的两段中详细介绍 "default relationship"。)
完全不同的是 a.prototype
-- 在 JavaScript 中,任何函数都可以是构造函数,即可以用 new
调用它。每当使用 new
调用函数时,它都会创建一个新对象,其 __proto__
是函数的 prototype
并通过 this
指向新创建的对象。所以在对 new a()
的调用中,this.__proto__
确实等于 a.prototype
。在定义函数 a
时,会自动创建此原型对象并将其存储在 a.prototype
中。
a.prototype.constructor
等于 a
,因为 JavaScript 内部例程为新定义的函数(如前一段所述)创建 prototype
对象总是给新原型一个 constructor
属性 来引用新定义的函数。要真正了解杂草,相关的 ECMAScript 例程是 19.2.1.1.1, CreateDynamicFunction,其中指出,"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."
a
没有自己的 constructor
属性,但它会自动继承 a.__proto__.constructor
可作为 a.constructor
访问,就像它继承任何其他属性一样在其原型父级上(就像 a.bind
实际上是 a.__proto__.bind
)。
最后,a.__proto__.constructor !== Object.prototype.constructor
因为 Object.prototype
不是函数对象的原型父级,而 Function.prototype
是。 a.__proto__.constructor === Function.prototype.constructor
(更简洁地说,a.__proto__ == Function.prototype
和 a.__proto__.constructor == Function
)是正确的。
我是 Javascript 的新手,我正在尝试深入挖掘并理解继承、构造函数和原型链。 所以,我创建了一个构造函数,
var a = function(){this.integer=1,this.float=1.0,this.string="one"}
现在,这个函数有一个 prototype.constructor 属性,一个构造函数 属性 和一个 __proto__.constructor
属性。
我的理解是__proto__.constructor
==执行的构造函数创建函数a。
此外,prototype.constructor 是我使用 new 关键字创建 'a' 实例时执行的函数。
但是我不明白第三个构造函数 属性 是干什么用的。它等于 __proto__.constructor
.
另外,b.__proto__.constructor
!== Object.prototype.constructor
,正如我所想的那样。这是为什么?
a.__proto__
是Function
原型(即Function.prototype
)。它是所有函数继承函数特定方法的对象,如 call
、apply
、bind
等。 a.__proto__.bind == a.bind
.
a.__proto__.constructor
是 Function
构造函数,即函数 Function
。 Function
原型通过 constructor
属性 引用其关联的构造函数,这始终是原型对象和构造函数之间的默认关系。 (在接下来的两段中详细介绍 "default relationship"。)
完全不同的是 a.prototype
-- 在 JavaScript 中,任何函数都可以是构造函数,即可以用 new
调用它。每当使用 new
调用函数时,它都会创建一个新对象,其 __proto__
是函数的 prototype
并通过 this
指向新创建的对象。所以在对 new a()
的调用中,this.__proto__
确实等于 a.prototype
。在定义函数 a
时,会自动创建此原型对象并将其存储在 a.prototype
中。
a.prototype.constructor
等于 a
,因为 JavaScript 内部例程为新定义的函数(如前一段所述)创建 prototype
对象总是给新原型一个 constructor
属性 来引用新定义的函数。要真正了解杂草,相关的 ECMAScript 例程是 19.2.1.1.1, CreateDynamicFunction,其中指出,"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."
a
没有自己的 constructor
属性,但它会自动继承 a.__proto__.constructor
可作为 a.constructor
访问,就像它继承任何其他属性一样在其原型父级上(就像 a.bind
实际上是 a.__proto__.bind
)。
最后,a.__proto__.constructor !== Object.prototype.constructor
因为 Object.prototype
不是函数对象的原型父级,而 Function.prototype
是。 a.__proto__.constructor === Function.prototype.constructor
(更简洁地说,a.__proto__ == Function.prototype
和 a.__proto__.constructor == Function
)是正确的。