Thursday 15 September 2011

Javascript - Looping through classes and adding functions -



Javascript - Looping through classes and adding functions -

i'm trying create html5 canvas game , want able attach functions buttons activate when clicked. can unique functions i'm struggling find way when looping through many buttons predefined function.

i've created illustration show i've tried far:

jsfiddle: http://jsfiddle.net/ra1rb74w/1/

// class want create array of myclass = function() { this.afunction; }; myclass.prototype = { // add together new function class addfunction: function (newfunction) { this.afunction = newfunction; }, // utilize current function usefunction: function () { if (this.afunction != null) { this.afunction; } } }; // base of operations function utilize in classes var basefunction = function(x) { console.log(x); } // create array of classes var myclasses = []; // add together 10 classes array , add together function each of them (var x = 0; x < 10; x++) { myclasses.push(new myclass()); myclasses[x].addfunction(basefunction(x)); } // utilize function in first class myclasses[0].usefunction();

you can see functions triggered don't want, , usefunction() function doesn't work. there way this?

so triggering basefunction calling basefunction(x). need either basefunction homecoming function can executed:

// class want create array of myclass = function() { this.afunction; }; myclass.prototype = { // add together new function class addfunction: function (newfunction) { this.afunction = newfunction; }, // utilize current function usefunction: function () { if (typeof this.afunction === "function") { this.afunction.call(this); } } }; // base of operations function utilize in classes var basefunction = function(x) { homecoming function() { console.log(x); }; } // create array of classes var myclasses = []; // add together 10 classes array , add together function each of them (var x = 0; x < 10; x++) { myclasses.push(new myclass()); myclasses[x].addfunction(basefunction); } // utilize function in first class myclasses[3].usefunction();

jsfiddle

or add together parameter addfunction can called addfunction(basefunction, x):

// class want create array of myclass = function() { this.afunction; }; myclass.prototype = { // add together new function class addfunction: function (newfunction, value) { this.afunction = newfunction; this.x = value; }, // utilize current function usefunction: function () { if (typeof this.afunction === "function") { this.afunction.call(this, this.x); } } }; // base of operations function utilize in classes var basefunction = function(x) { console.log(x); } // create array of classes var myclasses = []; // add together 10 classes array , add together function each of them (var x = 0; x < 10; x++) { myclasses.push(new myclass()); myclasses[x].addfunction(basefunction, x); } // utilize function in first class myclasses[3].usefunction();

jsfiddle

note changed check afunction == null function passed in may null, or string, or else. want check if executable.

javascript function loops

No comments:

Post a Comment