javascript - 为什么我在“console.log”对象时看不到从父对象继承的那些属性;在javascript中
问题描述
大家好,我最近正在学习 JavaScript 中的继承。我创建一个Child
函数并使其继承该Human
函数。我为then创建一个child
实例。Child
console.log(child)
我想知道为什么输出只有属性Child
,但没有Human
属性。
我想这是因为属性描述符,特别是enumerable
,但我无法弄清楚。谁能帮我?
const Child = function() {
this.name = "child"
}
const Human = function() {
this.move = "walking";
}
Child.prototype = new Human();
Child.prototype.constructor = Child;
const child = new Child();
console.log(child);
运行上面的代码后,我只看到 {name: "child"},虽然console.log(child.move)
给出了'walking'。
解决方案
简短的回答是,move
在实例上找不到Child
,而是在它继承自的原型上找到Human
。
让我们分析一下您的代码。
const Child = function() {
this.name = "child"
}
const Human = function() {
this.move = "walking";
}
这些是不言自明的,您创建了两个构造函数。它们是无关的(还)。
Child.prototype = new Human();
Child.prototype.constructor = Child;
这是继承部分。Child.prototype = new Human();
将创建 的实例Human
,并将其分配给Child.prototype
,因为该实例有一个成员move
,该成员被分配给Child.prototype
并且您得到本质上的Child.prototype = { move: 'walking' }
或Child.prototype.move = 'walking'
(那些不准确,但足够接近)
然后你将Child
自己指定为prototype.constructor
.
您看到奇怪行为的原因是您希望move
成为实例成员,但它是原型成员。这种效果的一个更显着的缺点是更改child.move
会同时针对所有子实例更改它,这不是您对实例成员的期望。
出于这个原因,不建议通过实际创建实例来进行继承,就像你做的那样,而是使用Object.create()
,像这样:
Child.prototype = Object.create(Human.prototype);
Child.prototype.constructor = Child;
此外,您的Child
函数应该调用父构造函数,以维护父的逻辑和成员。像这样:
const Child = function() {
Human.call(this); // call to parent constructor
this.name = "child";
}
完整代码:
const Human = function() {
this.move = "walking";
}
const Child = function() {
Human.call(this);
this.name = "child"
}
Child.prototype = Object.create(Human.prototype);
Child.prototype.constructor = Child;
const child = new Child();
console.log(child);
一旦你理解了原型链是如何工作的,请记住创建 ES6 类是为了更优雅地处理这些情况并使用更易读的代码:
class Human {
constructor() {
this.move = "walking";
}
}
class Child extends Human {
constructor() {
super();
this.name = "child";
}
}
推荐阅读
- c# - 迭代每个元素的子元素并比较内部文本
- wordpress - 在管理仪表板的一处添加分类术语
- python - 熊猫申请排
- android - 如何修复无法解决:support-compat
- interrupt - 8051 中断(硬件)能力
- c# - 如何在发布到网络位置时在启动时自动更新 .net 应用程序
- c# - Xceed Docx 表格单元格中的段落间距错误
- angular5 - 当我安装 angular2-image-gallery 包时,它会在节点模块的文件导入 rxjs/internal/Subscription 中引发错误
- java - url html表单在webview android中打开
- r - 在模型公式中引用许多匹配正则表达式的列的简洁方法