简体   繁体   English

使用John Resig的“简单JavaScript继承”,如何在一个方法中调用一个超级方法以及额外的代码?

[英]Using John Resig's “simple JavaScript inheritance” how can I call a super method PLUS extra code from within a method?

I decided to try out JavaScript genius John Resig's "simple JavaScript inheritance" as detailed on this blog page: 我决定尝试JavaScript天才John Resig的“简单JavaScript继承”,如以下博客页面所述:

http://ejohn.org/blog/simple-javascript-inheritance/ http://ejohn.org/blog/simple-javascript-inheritance/

I'm curious how one might override a method with code that calls the super method. 我很好奇一个人怎么可能用调用super方法的代码覆盖一个方法。 In other words, let's say I start with a Person class: 换句话说,假设我从一个Person类开始:

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 : 我扩展了Person类以创建一个新的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. init方法的两个版本中都有重复的代码。 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. 好像我应该能够从英雄类的init方法中调用Person类的init方法,然后在代码与职业属性的额外的行抛出。

I can't do that with Mr. Resig's code, though. 不过,我无法使用Resig先生的代码来做到这一点。 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. Person调用以创建Worker类的extend方法看到* this._super(arguments)*时,它将用Person的 init替换整个Worker的 init ,使我拥有未定义的职业属性。

Does anyone have any suggestions as to how to get around this without having to modify Mr. Resig's code? 是否有人对如何解决此问题而无需修改Resig先生的代码有任何建议? 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. 更新:我意识到我在执行Resig先生的代码时犯了一个小错误,这就是为什么它表现出我描述的方式的原因。 @chuckj correctly pointed out an error in Worker's init , too. @chuckj也正确指出了Worker的 init错误。

Change the Worker definition to, 将工作程序定义更改为

var Worker = Person.extend({ 
    init: function (name, age, occupation) { 
        this._super(name, age); 
        this.occupation = occupation; 
    } 
}); 

you don't pass the arguments array, you call the _super with the parameters it expects. 您不传递arguments数组, _super使用所需的参数调用_super

It seems your goal is to proxy the arguments to this._super . 看来您的目标是将arguments代理到this._super In this case you can just apply() them : 在这种情况下,您可以apply()

var Worker = Person.extend({
    init: function ( name, age, occupation ) {
        this._super.apply(this, arguments);
        this.occupation = occupation;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM