简体   繁体   中英

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

I'll be honest, I'm in way over my head with this one. Any help would be appreciated.

OBJECTIVE

I want to call a function called restart, but the function is inside an object and is being passed to a function Class.extend .

The code I'm working with is

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

What have I tried?

restart(); Doesn't work. I get TypeError: restart is not a function No biggy I didn't expect it to work. Based on this question > Javascript - Storing function in object - bad practice? I thought I could do something like Chickenz.restart(); but it's more complicated, because of Class.extend

I've included code for Class.extend towards the bottom of this question.

Later the restart function is called with the following code

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

I thought I would try self.restart(); but that didn't work- TypeError: self.restart is not a function.

So my question.

How can I call the restart function?

CODE for Class.extend

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;
 };

You appear to be basing this on John Resig's example here You should look at how he does it :)

Class.extend returns a constructor for a class. You can just create an instance of the class and then call it.

You need something like:

var chicken = new Chickenz();

chicken.restart();

Note that this will immediately throw an error right now though, because this will then be bound to the new chicken object, and the chicken doesn't have a score.restart function unless you have other code you aren't showing us. I have no idea what you want that line to do, but you'll need to add a score property with a restart method to the chicken object or have the restart method reference something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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