我更改了人类原型(prototype),现在检查属性时仍然未定义:
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 未定义的信息。。
这是为什么?
最佳答案
您应该已被分配 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);
关于javascript - 原型(prototype)中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33890340/