问题标签 [prototypal-inheritance]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
javascript - 在Javascript中继承对象时是否需要“Subclass.prototype.constructor = Subclass”?
考虑以下Student
继承自的示例Person
:
正如你所看到的,实例化一个Student
对象并调用它的方法工作得很好,但是Student.prototype.constructor
返回Person(name)
,这对我来说似乎是错误的。
如果我添加:
然后按预期Student.prototype.constructor
返回Student(name, id)
。
我应该总是添加Student.prototype.constructor = Student
吗?
你能在需要的时候举个例子吗?
javascript - 在Javascript中继承对象的两种不同(?)方法
Javascript中以下两种继承对象的方法有什么区别吗?
方法一:
方法二:
javascript - 原型继承对象的多个实例
我正在尝试掌握使用原型继承的方法。这是我下面的代码。它基于 Stoyan Stefanov 的“面向对象的 Javascript”一书,仅作了一些修改。
基本上我有一个Athlete
扩展对象的Person
对象。我创建了 3 个对象。Bob 是 a Person
,而 Billy Jean 和 Steve 是Athletes
。我按特定顺序添加了 Bob、Billy Jean 和 Steve。对于对象,我按以下特定顺序在所有 3 个对象中调用了say()
andrun()
函数和getSpeed()
and :Bob、Billy Jean 和 Steve。jump()
Athlete
这是下面的代码。
然而,当我运行代码时,虽然我首先调用了 Billy Jean 的函数,但 Steve 的属性弹出了两次,这意味着 Steve 替换了 Billy Jean。输出如下所示。
我只是想知道是否有办法将比利让和史蒂夫分开,这样我就可以得到他们的详细信息而不是史蒂夫的详细信息两次?
如果不可能,那么我可以使用什么替代方法来解决这个问题?任何解决方案或帮助都会有很大帮助。
javascript - “this”关键字的继承和问题
我正在构建一个相当复杂的 Web 应用程序,它从用户进行初始选择的主菜单开始。这是我第一次尝试在 JavaScript 中使用继承的真正 OOP 方法,我遇到了第一个问题,即“this”关键字不是我期望的。我猜这是我的 OOP/继承方法存在更广泛问题的结果,所以我希望得到一个答案,它不仅可以告诉我如何解决这个单独的问题,还可以为我的一般方法提供更深入的反馈和建议。
我只会发布 JS 代码,因为我认为 HTML 不相关,但如果有必要,我当然也可以发布。
下面的代码定义了主类Select
。然后它创建一个Select
被调用的子类SelectNum
(看代码的末尾)。在SelectNum
中,我试图覆盖 的mouseover
方法Select
,但不是完全覆盖——我想先调用 super 的(Select's)方法,然后运行一些额外的代码。但是当这个子类的mouseover
方法运行时,我立即得到以下错误:
“未捕获的类型错误:无法调用未定义的方法‘停止’”
基本上,this.shine
是未定义的。
首先,我使用 O'Reilly 的 JavaScript: The Definitive Guide 中的以下代码:
我的代码:
编辑
Raynos 的回应奏效了。this.sup.mouseover()
不再抛出错误,并且运行了正确的代码。但是,我实际上需要创建一个SelectNum
名为SelectLevel
. 与SelectNum
覆盖其超类的mouseover()
方法不同,SelectLevel
不需要覆盖SelectNum
的mouseover()
方法:
使用此代码,该mouseover()
方法会被连续调用。我相信那是因为this
现在绑定到SelectLevel
对象,所以this.sup
在行this.sup.mouseover()
中SelectNum
总是指代SelectNum
,所以它只是不断地调用自己。
如果我删除 中的this.sup[k] = this.sup[k].bind(this);
绑定SelectLevel
,则会收到错误消息Uncaught TypeError: Cannot call method 'mouseover' of undefined
。它似乎this.sup.mouseover()
被连续调用,调用mouseover
原型链中每个对象的方法。当它达到 时Object
,就会引发此错误,因为当然,Object
它没有sup
属性。
似乎我可以通过删除 in 的this.sup[k] = this.sup[k].bind(this);
绑定来解决这个问题SelectLevel
,然后将 in 包装this.sup.mouseover()
在一个 if 语句中,该语句首先检查sup
属性,然后再调用mouseover()
它的方法: ie if(this.sup !== undefined)
,但这真的感觉不对。
最终,我认为我遗漏了一些关于如何在 JavaScript 中进行子类化的基本知识。虽然这些特定问题的解决方案确实揭示了原型继承在 JS 中的工作原理,但我真的认为我需要在更广泛的层面上更好地理解。
javascript - Javascript - 如果超类构造函数接受参数,如何初始化超类
考虑以下 Javascript 片段,
现在如果我创建一个子类的实例,该对象将不会初始化属性“a”和“x”。
虽然我理解为什么会发生这种情况,但子类执行由超类构造函数完成的初始化的最干净的方法是什么?
javascript - Getting the class name to show up in the console when using Resig's Simple JavaScript Inhertiance
I'm using Resig's Simple JavaScript Inheritance to create my classes. The only thing I don't like about it so far is that when I log an object created with this library to the console, it's name is simply "Class". My question is whether there is a way to modify his code so that I get the actual class name in the console instead. Here's an example from Chrome's console:
I would really like that name "Class" to be the actual name of the class I've created, in the way it would if you did the following:
I believe I know the reason why this happen's with Resig's library: the actual constructor function is simply named "Class". Here's the code for his library:
You'll find the Class()
function about 2/3 of the way down. Does anyone know how to modify this code so that you get the actual name of the class in the console?
javascript - javascript 类继承自 Function 类
我喜欢在 javascript 中这样做,我可以创建一个函数,然后向该函数添加更多方法和属性
我想创建一个类来生成这些对象。我假设我必须从 Function 基类继承。
换句话说,我想:
但我不知道如何创建 myFunctionClass。我尝试了以下方法,但它不起作用:
我还尝试了这里找到的更复杂(也更合适?)的继承方法:如何“正确”在 JavaScript 中创建自定义对象?,没有更多的运气。我还尝试使用 node.js 中的 util.inherits(myFunctionClass, Function)。仍然没有运气
我已经用尽了谷歌,因此觉得我一定遗漏了一些基本的或明显的东西。帮助将不胜感激。
javascript - JavaScript 函数称为对象,带有小写“o”
我刚刚观看了 Douglas Crockford 的介绍性 JavaScript讲座,他在其中提到了一个名为的函数,该函数object
应该用于创建一个链接到作为其参数的对象的新对象。我猜他的意思是如果我说
的objB
内部[[prototype]]
参考设置为objA
,尽管他没有明确地这样表述。
另一方面,我读过他的书,他根本没有提到这样的函数,而是提出了他自己的从原型创建对象的方法,定义了以下函数:
本质上利用了操作符的行为,它将新创建的对象new
的内部链接设置为构造函数的属性指向的任何内容。[[prototype]]
prototype
我的问题是他为什么要省略内置函数并发明自己的方式来做同样的事情。以前的object
函数调用真的等同于
还是有一些细微的差别?
javascript - Crockford 原型继承的小缺点
只是在 JS 中尝试了不同的继承技术,并遇到了一些关于 Crockford 的原型继承模式的问题:
一切都很好-除非您登录到控制台-对象显示为F。我已经看到了可以重新指向构造函数的经典仿真-是否有类似的方法来强制对象(控制台)引用?
javascript - 克隆或应用:哪个“更好”?
我想创建一系列从基础对象继承或复制实例属性的对象。这使我决定使用哪种模式,我想询问您对哪种方法“更好”的意见。
它们都实现了相同的目标,即基于通用模板的唯一实例属性 - 问题是哪个更“优雅”