javascript - 原型(prototype)中未定义

标签 javascript

我更改了人类原型(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/

相关文章:

javascript - 如何将当前连接的设备名称获取到计算机?

javascript - 如何判断内联对象标签是否已加载其数据

javascript - 使用 Ng-Repeat 从指令中观察 Controller 中函数的输出

javascript - 为什么用 jQuery 在 head 中插入脚本如此奇怪?

javascript - 如何阻止一个人多次加载一个函数jquery

javascript - 当值为字符串时,kendo.tostring 不格式化数字

javascript - javascript 中的 mutex_lock 等效项

javascript - 为什么 `HTMLelement.innerText` 添加换行符(\n)?

c# - 如何在服务器 ASP.NET 上请求客户端 cookie

javascript - 导入 SASS 文件的测试组件时出现语法错误