问题标签 [javascript-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.

0 投票
2 回答
100 浏览

javascript - Javascript sub classing, super constructors and using custom extend loses base class methods

In another SO question about whether to call a super-constructor or use the prototype chain the answer provided by one of the users seemed sensible but didn't work for me when I implemented it on my solution which has multiple layers of inheritance.

Here's the answer I'm referring to: https://stackoverflow.com/a/4389429/392591

So I've created a jsFiddle using this custom extend method to illustrate the problem: https://jsfiddle.net/68q7yghv/
I've added the jsFiddle code at the bottom.

In my test I've created a 4 tier class inheritance structure from baseClass through to subSubSubClass each of which (except the baseClass) use the 'extend' method to inherit from the previous sub-class. When you hit the test button I'm printing out class.baseClassMethod to see if it finds it, but it only finds it for the base class.

You should see the following results: function () { this.output += "baseClassMethod"; }
undefined
undefined
undefined

Is it possible that the cause is that the call to the super constructor is messing with the 'this' context?

0 投票
9 回答
42418 浏览

javascript - 如何使用 ES6 类扩展函数?

ES6 允许扩展特殊对象。所以可以从函数继承。这样的对象可以作为函数调用,但我如何实现这样的调用的逻辑呢?


类的任何方法都通过this. 但是当它作为函数调用时,this指的是window. 当它作为函数调用时,如何获取对类实例的引用?

PS:同样的问题在俄语中。

0 投票
1 回答
385 浏览

javascript - ES6 类扩展原生类型使得 instanceof 在某些 JavaScript 引擎中表现异常?

考虑以下 ES6 类:

我的理解是两者都应该是true,在 Firefox 和 Chrome 中它们是,但是 Node 说es instanceof ExtendStringfalse。其他构造函数也是如此,而不仅仅是String.

我用过的软件:

  • --harmony带有标志的节点 v5.11.0 。
  • 铬 50
  • 火狐 45

哪个 JavaScript 引擎是正确的,为什么?

0 投票
0 回答
19 浏览

javascript - 寄生组合继承,重写inheritPrototype函数。

下面是寄生组合继承的经典示例。我可以重写inheritPrototype() 如下吗?另外,为什么需要在函数内部设置“构造函数”?

经典示例

0 投票
1 回答
43 浏览

javascript - 关于 Prototype 和 Object.create 模式的说明

我有一个动物课。它有sayName方法。

PrototypeCat1 使用模式从 Animal 继承。

Object.createCat2 使用模式从 Animal 继承。

在 cat1 的情况下,是否可以调用 sayName likecat1.sayName("Cat1")而不是cat1.prototype.sayName("Cat1");

在cat2的情况下,是否可以定义Cat2自己的属性和功能,然后像继承Animal一样

这样我就可以有一个干净的结构。我不想像这样使用

请说清楚。

0 投票
0 回答
70 浏览

javascript - Javascript多重继承说明

我是 Javascript 中的 OOP 新手。我正在学习继承。下面是我的代码。我在这里没有使用任何原型和创建函数,但仍然设法继承。我知道这不是正确的做法。但是为了实现我的目标,我应该如何重写代码以利用实际的继承。

编辑:但是当我这样尝试时,它会抛出错误。

0 投票
1 回答
168 浏览

javascript - 如何设置函数的原型

我试图理解 Javascript 的原型。我知道我可以单独向原型添加功能Calculator.prototype.calculate = function(){};,但是当我尝试设置新原型时,一切都崩溃了。calc.prototype 返回未定义。我的问题是为什么我们不能设置一个新的对象作为原型?如何将方法批量添加到原型中,而不是一一添加?

0 投票
3 回答
3223 浏览

javascript - Object.create 与 new

以下代码在我使用构造函数创建对象时有效,但是当我执行 object.Create 时,它​​没有正确初始化。functionName is not a function. 我有两个问题。为什么 object.create 不工作?

我将如何在同一个 Calculator Function 中组织我的代码,以便我可以同时使用 new 和 object.create ?

我知道我可以将方法添加到 Calculator.prototype 并执行 Object.create 但我想知道我的代码是否可以在当前结构中更改以允许两者?

0 投票
2 回答
224 浏览

javascript - 与 Javascript 继承混淆

我对javascript继承感到困惑。

考虑以下代码:

现在,该child1对象是否可以访问firstnamelastname属性?我可以访问该Greetings方法(因为它在原型中)。如果我尝试访问这些,它会显示为undefined. 必须进行哪些更改才能访问这些变量?

0 投票
1 回答
26 浏览

javascript - 原型继承没有按预期工作

根据原型继承,oldobj 与 newobj 函数没有链接。但是在上面的例子中,oldobj 是如何访问 newobj 的 Fifthm() 的呢?