簡體   English   中英

如何調用存儲在作為另一個函數的參數的對象中的函數?

[英]How can I call a function stored in an object that is being used as an argument for another function?

老實說,我已經在這個問題上走了過來。 任何幫助,將不勝感激。

目的

我想調用一個名為restart的函數,但該函數位於一個對象中,並被傳遞給一個函數Class.extend

我正在使用的代碼是

    var Chickenz = Class.extend(
{
        init: function(value) 
        {
           //do something
        },
        // restart game
        restart: function() 
        {
            this.score.restart();  
            //etc
        }
});

我試過了什么?

restart(); 不行。 我得到TypeError: restart is not a function沒有biggy我沒想到它工作。 基於這個問題> Javascript - 在對象中存儲功能 - 不好的做法? 我以為我可以做類似Chickenz.restart();事情Chickenz.restart(); 但由於Class.extend,它更復雜

我在這個問題的底部包含了Class.extend的代碼。

稍后使用以下代碼調用restart函數

/* GUI Events */
            // restart button
            addEvent($("restart"), "click", function() {
                self.restart();
                return false;
            });

我以為我會嘗試self.restart(); 但是沒有用 - TypeError: self.restart is not a function.

所以我的問題。

如何調用重啟功能?

Class.extend的CODE

var initializing = false;
  // The base Class implementation (does nothing)
  this.Class = function(){};
  // Create a new Class that inherits from this class
// Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype,
        prototype,
        name,
        tmp,
        ret;

    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    prototype = new this();
    initializing = false;

    // Copy the properties over onto the new prototype
    for ( name in prop ) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" ?
        (function(name, fn){
         return function() {
           tmp = this._super;
           // Add a new ._super() method that is the same method
           // but on the super-class
           this._super = _super[name];
           // The method only need to be bound temporarily, so we
           // remove it when we're done executing
           ret = fn.apply(this, arguments);
           this._super = tmp;
           return ret;
         };
        })(name, prop[name]) :
        prop[name];
    }
    // The dummy class constructor
    //Changed according to http://ejohn.org/blog/simple-class-instantiation/    
    //Use the new operator to instantiation                                     
        function Class(args){
                if ( this instanceof arguments.callee ) {
                    if ( !initializing && this.init )
                        this.init.apply( this, args.callee ? args : arguments );
                } else
                    return new arguments.callee( arguments );
            };

    // Populate our constructed prototype object
    Class.prototype = prototype;
    // Enforce the constructor to be what we expect
    Class.constructor = Class;
    // And make this class extendable
    Class.extend = arguments.callee;
    return Class;
 };

你似乎是基於John Resig 在這里的例子你應該看看他是如何做到的:)

Class.extend返回類的構造函數。 您可以只創建該類的實例,然后調用它。

你需要這樣的東西:

var chicken = new Chickenz();

chicken.restart();

請注意,這會立即引發錯誤,因為this將綁定到新的雞對象,並且雞沒有score.restart函數,除非您有其他代碼沒有向我們展示。 我不知道您想要該行做什么,但是您需要將一個帶有重啟方法的score屬性添加到chicken對象,或者讓restart方法引用其他內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM