我最近开始阅读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
Run Code Online (Sandbox Code Playgroud)
所以我在firebug中运行命令"var A = function(){};" 然后是"A.Constructor",它揭示了它的功能.我理解这一点.
我运行代码"A.prototype.constructor = A;" 我认为这会将A构造函数从Function更改为A.
A的构造函数属性已经改变了吗?相反,当我运行"A.constructor"时,它仍然给我function().
重点是什么?
我也看到A.constructor.prototype.constructor.prototype ..发生了什么?
xda*_*azz 12
如果A继承B使用A.prototype = new B();,则需要重置A类的构造函数属性A.prototype.constructor=A;,否则A的实例将具有B的构造函数.
在你的情况下, A.prototype.constructor === A将返回true,所以A.prototype.constructor = A什么也没做.