gpt4 book ai didi

javascript - Shell 中 BaseObject.Prototype 的 Node 打印 'undefined'

转载 作者:太空宇宙 更新时间:2023-11-04 02:22:38 25 4
gpt4 key购买 nike

我一直在谷歌上搜索这个问题,但我似乎找不到满意的答案。我意识到我要么在 Node 上做错了什么,要么完全不知道原型(prototype)继承是如何工作的,但对我来说,这只是让我发疯,不知道为什么,所以我很感激任何反馈来帮助我理解我做错了什么。

我有三门课。这些类可以做任何事情,但为了便于讨论,它们将具有名称。

  • 哺乳动物.js
  • Human.js
  • 测试.js

Mammal是基类,Human是子类,test是执行代码的类。现在,我知道有多种方法可以扩展另一个对象。无论是使用 Object.create() 还是构造函数配置。在本例中,我使用构造函数来创建新对象,并将子类的原型(prototype)直接指向它继承的对象。因此,如果子类在子类中找不到该方法,则默认情况下应该沿着原型(prototype)链向上到达它所继承的对象。

现在,当我这样做时,它起作用了......有点像?我可以调用其中一个函数,它的行为就好像它已被覆盖一样,但是当我尝试通过 Node 中的 console.log() 检查对象、原型(prototype)或任何信息时,我几乎什么也得不到。这让我非常困惑,我很乐意帮助澄清原因。

下面是我正在使用的类和输出。

哺乳动物.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;
})();

Human.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;
})();

测试.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 类的方法时,它不会将人类对象输出为一组空括号。它不会显示原型(prototype)引用吗?很明显,它的原型(prototype)链中有一些方法,因为它执行 talk 命令没有任何问题。

那么我在这里误解了什么?

最佳答案

啊。有人帮我澄清了这个问题。我将 human.prototype 属性与对象的 _ prototype _ 属性混为一谈。后者是当在当前对象中找不到该方法时将要遍历的引用。因此,它返回未定义的原因是有道理的。因为 human.prototype 与 human._ prototype 完全分开。建议的解决方案也是使用 Object.getPrototypeOf(object),但根据 MDN 的说法,它在旧版 Opera 浏览器中不起作用。所以,请注意!

关于javascript - Shell 中 BaseObject.Prototype 的 Node 打印 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32387162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com