使用 John Resig 的“简单 JavaScript 继承”如何从方法中调用超级方法以及额外的代码?
我决定尝试 JavaScript 天才 John Resig 的“简单 JavaScript 继承”,详细信息请参见此博客页面:
http ://ejohn.org/blog/simple-javascript-inheritance/
我很好奇如何使用调用 super 方法的代码重写方法。换句话说,假设我从一个 Person 类开始:
var Person = Class.extend({
init: function ( name, age ) {
this.name = name;
this.age = age;
}
});
我扩展该 Person 类来创建一个新类 Worker:
var Worker = Person.extend({
init: function ( name, age, occupation ) {
this.name = name;
this.age = age;
this.occupation = occupation;
}
});
存在代码重复在 init 方法的两个版本中。无论我使用哪个类,都会执行以下两行:
this.name = name;
this.age = age;
看来我应该能够从 Hero 中调用 Person 类的 init 方法 类的 init 方法,然后使用 ocupation 属性添加额外的代码行。
不过,我无法用 Resig 先生的代码做到这一点。以下内容不起作用:
var Worker = Person.extend({
init: function ( name, age, occupation ) {
this._super(arguments);
this.occupation = occupation;
}
});
一旦从 Person 调用创建 Worker 类的 extend 方法看到 *this._super(arguments) * 它将整个 Worker's init 替换为 Person's init ,留下了一个未定义的职业 em> 属性。
有没有人对如何解决这个问题有任何建议,而无需修改 Resig 先生的代码?我目前正在尝试不同的方法来实现“超级”的概念,但我无法让它与现有代码一起工作这一事实一直困扰着我。 :-)
更新:我意识到我在实现 Resig 先生的代码时犯了一个小错误,这就是为什么它的行为如我所描述的那样。 @chuckj 也正确地指出了 Worker init 中的错误。
I decided to try out JavaScript genius John Resig's "simple JavaScript inheritance" as detailed on this blog page:
http://ejohn.org/blog/simple-javascript-inheritance/
I'm curious how one might override a method with code that calls the super method. In other words, let's say I start with a Person class:
var Person = Class.extend({
init: function ( name, age ) {
this.name = name;
this.age = age;
}
});
I extend that Person class to create a new class Worker:
var Worker = Person.extend({
init: function ( name, age, occupation ) {
this.name = name;
this.age = age;
this.occupation = occupation;
}
});
There's repetition of code in the two versions of the init method. The following two lines are executed no matter which class I'm using:
this.name = name;
this.age = age;
It seems like I should be able to call the Person class's init method from within the Hero class's init method and then throw in the extra line of code with the occupation property.
I can't do that with Mr. Resig's code, though. The following doesn't work:
var Worker = Person.extend({
init: function ( name, age, occupation ) {
this._super(arguments);
this.occupation = occupation;
}
});
As soon as the extend method called from Person to create the Worker class sees *this._super(arguments)* it replaces the entirety of Worker's init with Person's init leaving me with an undefined occupation property.
Does anyone have any suggestions as to how to get around this without having to modify Mr. Resig's code? I'm currently trying out different ways to implement the concept of a "super," but the fact that I can't get this to work with the existing code is stuck in my head. :-)
UPDATE: I realized that I made one small error in implementing Mr. Resig's code which is why this behaved the way I described. @chuckj correctly pointed out an error in Worker's init, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将 Worker 定义更改为,
您不传递
arguments
数组,而是使用它期望的参数调用_super
。Change the Worker definition to,
you don't pass the
arguments
array, you call the_super
with the parameters it expects.看来您的目标是将
参数
代理到this._super
。在这种情况下,您只需apply()
它们:It seems your goal is to proxy the
arguments
tothis._super
. In this case you can justapply()
them :