Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a callback function inside a javascript class

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?

like image 395
ed209 Avatar asked Nov 27 '11 14:11

ed209


People also ask

How do you pass a callback function in JavaScript?

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.

How do you assign a callback 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.

Can we use nested callbacks?

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.

Where we can use callback function in JavaScript?

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.


2 Answers

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");
like image 172
Zoidberg Avatar answered Sep 20 '22 21:09

Zoidberg


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 );
like image 42
JJJ Avatar answered Sep 21 '22 21:09

JJJ