简体   繁体   English

如何调用存储在作为另一个函数的参数的对象中的函数?

[英]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 . 我想调用一个名为restart的函数,但该函数位于一个对象中,并被传递给一个函数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. 我得到TypeError: restart is not a function没有biggy我没想到它工作。 Based on this question > Javascript - Storing function in object - bad practice? 基于这个问题> Javascript - 在对象中存储功能 - 不好的做法? I thought I could do something like Chickenz.restart(); 我以为我可以做类似Chickenz.restart();事情Chickenz.restart(); but it's more complicated, because of Class.extend 但由于Class.extend,它更复杂

I've included code for Class.extend towards the bottom of this question. 我在这个问题的底部包含了Class.extend的代码。

Later the restart function is called with the following code 稍后使用以下代码调用restart函数

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

I thought I would try self.restart(); 我以为我会尝试self.restart(); but that didn't work- TypeError: self.restart is not a function. 但是没有用 - TypeError: self.restart is not a function.

So my question. 所以我的问题。

How can I call the restart function? 如何调用重启功能?

CODE for Class.extend 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;
 };

You appear to be basing this on John Resig's example here You should look at how he does it :) 你似乎是基于John Resig 在这里的例子你应该看看他是如何做到的:)

Class.extend returns a constructor for a class. Class.extend返回类的构造函数。 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. 请注意,这会立即引发错误,因为this将绑定到新的鸡对象,并且鸡没有score.restart函数,除非您有其他代码没有向我们展示。 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. 我不知道您想要该行做什么,但是您需要将一个带有重启方法的score属性添加到chicken对象,或者让restart方法引用其他内容。

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

相关问题 如何在JavaScript中使用参数调用匿名函数(存储在字符串中)? - How can I call an anonymous function (stored in string) with an argument in JavaScript? 如何通过名称调用存储在另一个对象中的一个函数? - How can I call a function stored in one object on another object by name? 在jQuery对象上调用作为参数传递给另一个函数的函数 - Call a function, passed to another function as an argument, on a jQuery object 当我调用 function 时,如何在 function 中传递参数并将该参数定义为变量? - How can I pass an argument in a function and define that argument as a variable when I call the function? 如何从另一个 function 调用 function 以及如何? - How can I call a function from another function and how? 我如何使用 1 个参数在空手道功能文件中调用 js function - How can i call the js function in karate feature file with 1 argument 如何调用对象的函数,就像它是另一个对象的属性一样? - How do i call a function of an object as if it was the property of another object? JS如何在另一个函数中调用一个函数? - JS How can I call a function within another function? 如何在Node.js中从另一个函数调用一个函数 - How can I call one function from another function in nodejs 如何从类中的另一个函数调用函数 - How can I call a function from another function inside a Class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM