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?
Passing a function to another function or passing a function inside another function is known as a Callback Function. Syntax: function geekOne(z) { alert(z); } function geekTwo(a, callback) { callback(a); } prevfn(2, newfn); Above is an example of a callback variable in JavaScript function.
A custom callback function can be created by using the callback keyword as the last parameter. It can then be invoked by calling the callback() function at the end of the function. The typeof operator is optionally used to check if the argument passed is actually a function. console.
When using callbacks, you're required to put dependent program logic that is to be executed after an asynchronous operation has completed inside a callback. When you combine multiple such calls, you end up with deeply nested code. Deeply nested callbacks in JavaScript code.
A JavaScript callback is a function which is to be executed after another function has finished execution. A more formal definition would be - Any function that is passed as an argument to another function so that it can be executed in that other function is called as a callback function.
You will need to change
setCallback = function (c) {callback = c;}
to
this.setCallback = function (c) {callback = c;}
so the setCallback function will be public.
If you also want to scope the callback, you can call it like this
callback.call(scope, param1, param2);
If you don't know how many parameters, you can call it like this
callback.apply(scope, parameters);
Scope could be any object, even an empty one {} if you want.
By the way, I really like your use of private variables in this example, great work with the javascript. Here is a good way to write your javascript object to help with the initialization and readability
var mynamespace = {};
(function () {
function MyObject(param1, param2) {
this.initialize(param1, param2);
}
MyObject.prototype = {
initialize: function (param1, param2) {
var privateScope = {
param1: param1,
param2: param2,
callback: null
};
this.setCallback = function (c) {
privateScope.callback = c;
}
this.doSomething = function () {
if (privateScope.callback) {
privateScope.callback.call();
}
}
}
}
mynamespace.MyObject = MyObject;
}());
Then to use it
var obj = new mynamespace.MyObject("value1", "value2");
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With