【发布时间】:2015-09-03 23:50:58
【问题描述】:
我一直在谷歌上搜索这个,而对于我的生活,我似乎无法找到一个令人满意的答案。我意识到我要么在 Node 上做错了,要么完全不知道原型继承是如何工作的,但对我来说,这只是让我发疯,不知道为什么,所以我很感激任何反馈来帮助我理解我在做什么错了。
我有三门课。这些类可以做任何事情,但为了便于讨论,它们会有名称。
- 哺乳动物.js
- Human.js
- test.js
Mammal 是基类,Human 是子类,test 是执行代码的类。现在,我知道有多种方法可以扩展另一个对象。无论是使用 Object.create() 还是构造函数配置。在这种情况下,我使用构造函数来创建新对象,并且我将子类的原型直接指向它所继承的对象。因此,如果子类在子类中找不到方法,则默认情况下子类必须沿着原型链向上到它所继承的对象。
现在,当我这样做时,它就可以工作了……有点?我可以调用其中一个函数,它的行为就好像它已被覆盖,但是当我尝试通过节点中的 console.log() 检查对象、原型或任何信息时,我几乎什么也得不到。这让我非常困惑,我很乐意帮助澄清为什么会这样。
以下是我正在使用的类和输出。
哺乳动物.js
module.exports = (function() {
function Mammal() {
// do nothing
}
Mammal.prototype = {};
Mammal.prototype.initialize = function initialize() {
throw Error(this.type() + ' has extended the "Mammal" class without overriding the "type" function. Please assign this function correctly in the new sub-class.');
};
Mammal.prototype.speak = function speak() {
// Mammal.speak.call(this);
console.log('I am a ' + this.type() + '.');
};
Mammal.prototype.type = function type() {
throw Error('A sub-object has extended the "Mammal" class without overriding the "type" function. Please assign this function correctly in the new sub-class.');
};
Mammal.prototype.printDebug = function printDebug() {
console.log(this.type() + " prototype: " + this.prototype);
};
return Mammal;
})();
人类.js
var Mammal = require('./Mammal');
module.exports = (function() {
function Human() {
// Call to super
Mammal.constructor.call(this);
};
Human.prototype = new Mammal();
Human.prototype.initialize = function initialize() {
// do something
};
Human.prototype.type = function type() {
return "Human";
};
return Human;
})();
test.js
var Human = require('./Human');
var human = new Human();
console.log("------------------------")
console.log("Object Inspection")
console.log("------------------------")
console.log(human);
console.log("------------------------")
console.log("Prototype Inspection")
console.log("------------------------")
console.log(human.prototype);
console.log("------------------------")
console.log("Stringify Inspection")
console.log("------------------------")
console.log(JSON.stringify(human.prototype));
console.log("------------------------")
console.log("Object printDebug")
console.log("------------------------")
human.printDebug();
console.log("------------------------")
console.log("Object printDebug")
console.log("------------------------")
console.log(human.type());
console.log("------------------------")
console.log("Speak")
console.log("------------------------")
human.speak();
输出
------------------------
Object Inspection
------------------------
{}
------------------------
Prototype Inspection
------------------------
undefined
------------------------
Stringify Inspection
------------------------
undefined
------------------------
Object printDebug
------------------------
Human prototype: undefined
------------------------
Object printDebug
------------------------
Human
------------------------
Speak
------------------------
I am a Human.
我的期望是 human.prototype 会指向某些东西。特别是声明的 Mammal 对象。除了它不将人类对象输出为一组空括号外,当它确实具有从它“扩展”的 Mammal 类中的方法时。它不会显示原型参考吗?很明显,它的原型链中有一些方法,因为它正在执行 speak 命令而没有任何问题。
那么我在这里误解了什么?
【问题讨论】:
标签: javascript node.js inheritance output prototypal-inheritance