为什么在OOP javascript中使用object.prototype.constructor

Why use object.prototype.constructor in OOP javascript?

本文关键字:object prototype constructor OOP javascript 为什么      更新时间:2023-09-26

我最近开始阅读OOP javascript,作者似乎忽略了一件事,那就是当一个对象A被声明时,我突然看到"A.prototype.constructor=A;例如,

var A = function(){}; // This is the constructor of "A"
A.prototype.constructor = A;
A.prototype.value = 1;
A.prototype.test = function() { alert(this.value); }
var a = new A(); // create an instance of A
alert(a.value);  // => 1

所以我在firebug中运行命令"var A=function(){};"然后是"A.Constructor",它揭示了它是一个函数。我理解这一点
我运行代码"A.prototype.constructor=A;",我认为这将A构造函数从Function更改为A.

A的构造函数属性已更改,对吗?相反,当我运行"A.constructor"时,它仍然会给我函数()。

有什么意义?

我还看到了一个构造函数。原型。构造函数。原型…发生了什么?

如果A使用A.prototype = new B();继承B,则需要使用A.prototype.constructor=A;重置类A的构造函数属性,否则A的实例将具有B的构造函数。

在您的情况下,A.prototype.constructor === A将返回true,因此A.prototype.constructor = A没有执行任何操作。

您可以快速测试出额外的分配完全无效:

var A = function() {};
A.prototype.constructor === A; // true -- why assign then?

只有当您为类分配了一个新的原型对象,覆盖了原始构造函数时,重置构造函数属性才有意义:

var A = function() {};
A.prototype = protoObject; // some object with members that you'd like to inherit
A.prototype.constructor = A; // reset constructor

在您的情况下,作者可能会盲目地将此作为良好的实践,即使在没有必要的情况下也是如此。

如果经常在JS经典继承模式中使用此代码(该代码来自Stoyan Stefanov的JavaScript Patterns):

function inherit(C, P) { 
    var F = function () {}; 
    F.prototype = P.prototype; 
    C.prototype = new F(); 
    C.uber = P.prototype; 
    C.prototype.constructor = C; 
} 

为子类分配正确的构造函数。

在您的情况下,它什么也没做,因为A.prototype.constructor === A在分配之前。

您可能想看看我对类似问题的回答:

https://stackoverflow.com/a/19616652/207661

TL;DR:构造函数不是实例自己的属性。因此,为了使事情看起来一致,JavaScript解释器需要将prototype.constructor设置为自己的函数。此功能可用于对许多不同类型的对象进行通用操作的函数中。

根据MDN,所有对象都从其原型继承构造函数属性:

Example 1:
var o = {};
o.constructor === Object; // true

Example2:
function Tree() {  
}    
var theTree = new Tree();
console.log(theTree.constructor === Tree ); // true

在运行时,它不会根据构造函数属性的值产生任何差异。

但是,由于构造函数属性返回对创建实例原型的Object函数的引用,因此在将新原型分配给Object函数时,应该重置构造函数属性。

var Forest = function() {};
Forest.prototype = theTree;  
console.log(new Forest().constructor === Tree ); // true
Forest.prototype.constructor = Forest;  
console.log(new Forest().constructor === Forest ); // true

https://jsfiddle.net/j1ub9sap/

详细信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor