【发布时间】:2015-07-28 11:02:36
【问题描述】:
function Plant() {
this.country = "Mexico"
this.isOrganic = true;
}
function Fruit(fName, fColor) {
this.name = fName;
this.color = fColor;
}
Fruit.prototype = new Plant();
var abanana = new Fruit("Banana", "Yellow")
console.log(abanana.constructor)
所以在我的代码中,我尝试使用原型继承。每次创建 Fruit 的新实例 (var aFruit = new Fruit()) 时,都会为新实例的原型分配来自 Fruit 构造函数的原型,即 Fruit.prototype。
那么为什么是 abanana.constructor 不是
[function: Fruit]
但是
[function: Plant]?
我认为这是构造函数的作用:
此外,所有从另一个对象继承的对象也继承了构造函数属性。而这个构造函数属性只是一个持有或指向对象构造函数的属性(就像任何变量一样)。
【问题讨论】:
标签: javascript