Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I call a function stored in an object that is being used as an argument for another function?
    primarykey
    data
    text
    <p>I'll be honest, I'm in way over my head with this one. Any help would be appreciated.</p> <p><strong>OBJECTIVE</strong></p> <p>I want to call a function called restart, but the function is inside an object and is being passed to a function <code>Class.extend</code>.</p> <p>The code I'm working with is</p> <pre><code> var Chickenz = Class.extend( { init: function(value) { //do something }, // restart game restart: function() { this.score.restart(); //etc } }); </code></pre> <p>What have I tried?</p> <p><code>restart();</code> Doesn't work. I get <code>TypeError: restart is not a function</code> No biggy I didn't expect it to work. Based on this question > <a href="https://stackoverflow.com/questions/8902687/javascript-storing-function-in-object-bad-practice">Javascript - Storing function in object - bad practice?</a> I thought I could do something like <code>Chickenz.restart();</code> but it's more complicated, because of Class.extend</p> <p>I've included code for Class.extend towards the bottom of this question.</p> <p>Later the restart function is called with the following code</p> <pre><code>/* GUI Events */ // restart button addEvent($("restart"), "click", function() { self.restart(); return false; }); </code></pre> <p>I thought I would try <code>self.restart();</code> but that didn't work- <code>TypeError: self.restart is not a function.</code></p> <p><strong>So my question.</strong> </p> <p>How can I call the restart function?</p> <p><strong>CODE for Class.extend</strong></p> <pre><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" &amp;&amp; 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 &amp;&amp; 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; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload