简体   繁体   English

在JavaScript类中调用父方法

[英]Call parent method in a JavaScript Class

before nothing I'm new and I'm glad to join to this great community. 在此之前,我是新人,我很高兴加入这个伟大的社区。 The thing is, I have a JavaScript class called Character like this: 问题是,我有一个名为Character的JavaScript类,如下所示:

var Character = function()

{

    // ..Some previous Properties / Methods (included one called "StareDown()")..

    // And this method:

    this.GrabDown = function()

    {
        Context.clearRect(0, 0, CanvasWidth, CanvasHeight);
        //Context.fillRect(0, 0, CanvasWidth, CanvasHeight);
        Context.drawImage(this.GrabbingDown, CharacterX, CharacterY, SpriteWidth, SpriteHeight);

        window.setTimeout
        (
            function()

            {
                // Here is where I want to call the parent class method "StareDown()", but I don't know how.                
            },
            250
        );
    }
}

So this is my big question, How can I access a parent method through that sub anonymous function? 所以这是我的一个大问题,如何通过该子匿名函数访问父方法? I've been trying to figure it out the whole night but I couldn't find some helpful information, Thank's! 我一直想弄清楚整个晚上,但我找不到一些有用的信息,谢谢!

You need to store your this object of the parent in a variable (assuming you have defined the function as this.StareDown = function () {...} 您需要将存储this父对象在一个变量(假设你已经定义的功能this.StareDown = function () {...}

var Character = function()

{

    // ..Some previous Properties / Methods (included one called "StareDown()")..
    this.StareDown = function() {...}

    var curCharacter = this;

    this.GrabDown = function()

    {
        Context.clearRect(0, 0, CanvasWidth, CanvasHeight);
        //Context.fillRect(0, 0, CanvasWidth, CanvasHeight);
        Context.drawImage(this.GrabbingDown, CharacterX, CharacterY, SpriteWidth, SpriteHeight);

        window.setTimeout
        (
            function()

            {
                // Here is where I want to call the parent class method "StareDown()", but I don't know how.       
                curCharacter.StareDown(...);         
            },
            250
        );
    }
}

You could just use window.setTimeout(this.StareDown,250); 你可以使用window.setTimeout(this.StareDown,250); but bare in mind that the method will be called in the global context (ie this will point to window , rather than the Character instance that invoked the GrabDown method. 但请记住,该方法将在全局上下文中调用(即,这将指向window ,而不是调用GrabDown方法的Character实例。

To use the function as an object method: 要将该函数用作对象方法:

window.setTimeout((function(that)
{
    return function()
    {
        return that.StareDown();
    }
})(this),250);

Should work. 应该管用。 This being rather verbose, perhaps looking at the MDN docs for call , apply and especially bind might prove useful 这是相当冗长的,也许看看MDN文档的callapply和特别bind可能证明是有用的

This is what I would have done: 这就是我要做的事情:

var Character = function () {
    this.GrabDown = function () {
        setTimeout(function () {
            // call this.StareDown in here
        }.bind(this), 250);
    };
};

This works because we're binding the the this pointer to the anonymous FunctionExpression passed to setTimeout . 这是有效的,因为我们将this指针绑定到传递给setTimeout的匿名FunctionExpression Thus it may be used like a normal method of the class. 因此,它可以像类的常规方法一样使用。

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

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