【发布时间】:2015-09-20 00:46:21
【问题描述】:
我不太明白,为什么我在第一条日志消息Prototype: [object Object],第二条是Prototype: New Plant?
function Plant() {
this.toString = function() {
return "Plant";
}
}
function Fruit(name) {
Plant.call(this);
this.name = name;
this.toString = function() {
return "Fruit";
}
}
Fruit.prototype = Object.create(Plant.prototype);
var apple = new Fruit("apple");
console.log("Prototype: " + Object.getPrototypeOf(apple));
Plant.prototype.toString = function() {
return "New Plant";
}
console.log("Prototype: " + Object.getPrototypeOf(apple));
我想既然我在 Plant 类中定义了 toString() 方法,那么所有 Fruit 实例也会有这个方法吗?
【问题讨论】:
标签: javascript inheritance overriding prototype