从子类调用超类方法 - JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13762320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:42:43  来源:igfitidea点击:

Calling Superclass Method From Subclass - JavaScript

javascriptinheritancecanvassubclasssuperclass

提问by Altherat

Possible Duplicate:
set attribute with javascript super method

可能的重复:
使用 javascript 超级方法设置属性

I am trying to create a simple game in HTML5 for fun. I have an Entity class that is supposed to be the superclass of the Player class.

我正在尝试用 HTML5 创建一个简单的游戏来获得乐趣。我有一个实体类,它应该是 Player 类的超类。

function Entity(x, y) {

    this.x = x;
    this.y = y;

    this.tick = function() {
        //Do generic stuff
    }
}

function Player(x, y) {

    this.parent.constructor.call(this, x, y);

    this.tick = function() {
        //Do player-specific stuff
        this.parent.tick.call(this);
    }
}

Player.prototype = new Entity();
Player.prototype.constructor = Player;
Player.prototype.parent = Entity.prototype;

The problem is at this line:

问题出在这一行:

this.parent.tick.call(this);

I get an error displayed in the JavaScript console of chrome: "Uncaught TypeError: Cannot call method 'call' of undefined".

我在 chrome 的 JavaScript 控制台中显示错误:“未捕获的类型错误:无法调用未定义的方法‘调用’”。

I don't get it and I've spent a long time trying to find posts of a similar issue. My call to the superclass' constructor works fine but the call to the superclass' tick method does not work.

我不明白,我花了很长时间试图找到类似问题的帖子。我对超类'构造函数的调用工作正常,但对超类'tick 方法的调用不起作用。

I'm very new to making games so I have no idea if this a good setup (calling superclass tick from subclass tick). If there is a better, more typical way that people use, please tell.

我对制作游戏很陌生,所以我不知道这是否是一个好的设置(从子类 tick 调用超类 tick)。如果有更好的、更典型的人们使用的方式,请告诉。

Thanks.

谢谢。

回答by Bergi

Adapting this answerto your code:

将此答案调整为您的代码:

function Entity(x, y) {

    this.x = x;
    this.y = y;

    this.tick = function() {
        //Do generic stuff
    }
}

function Player(x, y) {

    this.parent.constructor.call(this, x, y);

    var oldtick = this.tick;
    this.tick = function() {
        //Do player-specific stuff
        oldtick.call(this);
    }
}

Player.prototype = Object.create(Entity.prototype);
Player.prototype.constructor = Player;
Player.prototype.parent = Entity.prototype;

回答by Mark

Your question inspired me to look around and I found what I think is a great article by Josh Gertzenabout this concept.

你的问题激励我环顾四周,我发现我认为是Josh Gertzen关于这个概念的一篇很棒的文章

I blatantly copy from his article some code to set up an extendsmethod on classes:

我公然从他的文章中复制了一些代码来设置extends类的方法:

function Class() { }
Class.prototype.construct = function() {};
Class.extend = function(def)
{
    var classDef = function()
    {
        if (arguments[0] !== Class)
        {
            this.construct.apply(this, arguments);
        }
    };
    var proto = new this(Class);
    var superClass = this.prototype;
    for (var n in def)
    {
        var item = def[n];                      
        if (item instanceof Function) item.$ = superClass;
        proto[n] = item;
    }
    classDef.prototype = proto;
    classDef.extend = this.extend;      
    return classDef;
};

After which your case is as simple as:

之后,您的情况就像这样简单:

var Entity = Class.extend({
    tick: function()
    {
        alert('Entity tick');
    }
});

var Player = Entity.extend({
    tick: function()
    {
        alert('Player tick');
        arguments.callee.$.tick.call(this);
    }
});

p = new Player();
p.tick();

Which will alert Player tickand then Entity tick.

哪个会发出警报Player tick,然后Entity tick