JavaScript中几个重要的属性(this、constructor、proto(2)


Person.prototype = Object({
getName: () {
.name;
}
});


而constructor始终指向创建自身的构造函数,所以此时Person.prototype.constructor === Object,即是:

复制代码 代码如下:


Person(name) {
.name = name;
};
Person.prototype = {
getName: () {
.name;
}
};
p = Person();
console.log(p.constructor === Object);
console.log(Person.prototype.constructor === Object);
console.log(p.constructor.prototype.constructor === Object);


怎么修正这种问题呢?方法也很简单,重新覆盖Person.prototype.constructor即可:

复制代码 代码如下:


Person(name) {
.name = name;
};
Person.prototype = Object({
getName: () {
.name;
}
});
Person.prototype.constructor = Person;
p = Person();
console.log(p.constructor === Person);
console.log(Person.prototype.constructor === Person);
console.log(p.constructor.prototype.constructor === Person);

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdgjgd.html