我改变了人类原型,现在我在检查属性时仍然未定义:
function Human() {
this.name = "Default Human";
this.age = "Default Age";
this.height = 122;
}
function humanExtend() {
this.address = "Updated Address Sun";
this.power = "ShowBoat";
}
var banmeet = new Human();
console.log(banmeet.name);
Human.prototype = humanExtend;
var bradPitt = new Human();
console.log(bradPitt.power);
但是我得到了一个未定义的bradPitt.power。。
为什么?
答案 0 :(得分:2)
您应该已经被分配了HumanExtend的实例,如下所示 -
function Human() {
this.name = "Default Human";
this.age = "Default Age";
this.height = 122;
}
function humanExtend() {
this.address = "Updated Address Sun";
this.power = "ShowBoat";
}
var banmeet = new Human();
console.log(banmeet.name);
//Assign instance of HumanExtend here
Human.prototype = new humanExtend();
var bradPitt = new Human();
console.log(bradPitt.power);