- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在谷歌上搜索这个问题,但我似乎找不到满意的答案。我意识到我要么在 Node 上做错了什么,要么完全不知道原型(prototype)继承是如何工作的,但对我来说,这只是让我发疯,不知道为什么,所以我很感激任何反馈来帮助我理解我做错了什么。
我有三门课。这些类可以做任何事情,但为了便于讨论,它们将具有名称。
Mammal是基类,Human是子类,test是执行代码的类。现在,我知道有多种方法可以扩展另一个对象。无论是使用 Object.create() 还是构造函数配置。在本例中,我使用构造函数来创建新对象,并将子类的原型(prototype)直接指向它继承的对象。因此,如果子类在子类中找不到该方法,则默认情况下应该沿着原型(prototype)链向上到达它所继承的对象。
现在,当我这样做时,它起作用了......有点像?我可以调用其中一个函数,它的行为就好像它已被覆盖一样,但是当我尝试通过 Node 中的 console.log() 检查对象、原型(prototype)或任何信息时,我几乎什么也得不到。这让我非常困惑,我很乐意帮助澄清原因。
下面是我正在使用的类和输出。
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;
})();
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;
})();
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/
我只是有一个更琐碎的问题。 为什么undefined == undefined 返回true,而undefined >= undefined 为false? undefined 等于 undefine
用PHP 7.2编写套接字服务器。根据Firefox 60中的“网络”选项卡,服务器的一些HTTP响应的第一行随机变为undefined undefined undefined。因此,我尝试记录套接字
在 JavaScript 中这是真的: undefined == undefined 但这是错误的: undefined <= undefined 起初我以为<=运算符包含第一个,但我猜它试图将其转换
在回答这个问题 (Difference between [Object, Object] and Array(2)) 时,我在 JavaScript 数组中遇到了一些我以前不知道的东西(具有讽刺意味的
来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of , Note: thi
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
当我添加 到我的 PrimeFaces Mobile 页面,然后我在服务器日志中收到以下警告 WARNING: JSF1064: Unable to find or serve resource, u
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!