Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  109] [ 2]  / answers: 1 / hits: 22279  / 13 Years ago, sun, november 27, 2011, 12:00:00

excuse the pseudo code, my actual file is much larger:/



I want to call a function (with parameters) from inside a class. However, that function should be passed to the class as a variable.



someObject = {
itWorked:function(answer){
alert(answer);
},

plugins:{
somePlugin:function(){

var callback;
this.doSomething = doSomething;

function setCallback(c){
callback = c;
}

function doSomething(){
var answer = hello;
[callback](answer); // how do I call this?
}

}
},

widgets:{
something:function(){
var doIt = new someObject();
doIt.setCallback(someObject.itWorked()); // how do I send this?
doIt.doSomething();
}
}
}


So how would I pass itWorked() to the class?
And how would I call that itWorked(answer) function within the class as well as passing a variable to if?


More From » class

 Answers
11

Remove the parentheses to pass the function as a variable.



doIt.setCallback( someObject.itWorked );


You can then use the callback as you would any other function.



callback( answer );

[#88884] Friday, November 25, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alonso

Total Points: 747
Total Questions: 108
Total Answers: 105

Location: Mauritania
Member since Sun, Sep 25, 2022
2 Years ago
;