what javascript prototype.constructor for?

Possible Duplicate:
How does JavaScript .prototype work?

Here is the inheritance structure I setup for testing:

function A() {
    this.a = 1;
}

function B() {
    this.b = 2;
}

B.prototype = new A();
//B.prototype.constructor = B;

Below is what I tried in Chrome's JavaScript Console:

>var b = new B;
>b instanceof A
true

>B.prototype.constructor
function A() {
    this.a = 1;
}

My question is what's the purpose to setup B.prototype.constructor = B? Which I've already commented out? It doesn't seem to break the inheritance.

Thanks in advance.


B.prototype.constructor = B results in instanceof ' giving the expected results, but there are better ways of doing that. There is some discussion of this at: Convention for prototype inheritance in JavaScript

链接地址: http://www.djcxy.com/p/30088.html

上一篇: 控制台上功能与对象的原型属性的可见性

下一篇: 什么JavaScript的prototype.constructor的?