What does anObject.prototype.constructor do?

后端 未结 2 1750
攒了一身酷
攒了一身酷 2021-01-07 11:38

Suppose we have this constructor:

var Foo = function(){
   this.x = \"y\";
}

Foo and Foo.prototype.constructor ev

2条回答
  •  没有蜡笔的小新
    2021-01-07 12:03

    .prototype.constructor is just a helper property so that the objects created can easily refer the constructor with this.constructor, or as in your case i.constructor.

    Setting it arbitrarily to something else doesn't have any effect except on code that expects to get the constructor of an object with obj.constructor:

    if( obj.constructor === Foo ) {
     //It's a Foo
    }
    

    So it's a good convention just to leave it be.

    The es5shim relies on .constructor.prototype to shim Object.getPrototypeOf

提交回复
热议问题