Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - self-executing anonymous functions and callback

Tags:

javascript

Can I use callback with self-executing function?
If yes, have you seen any examples?

JavaScript self-executing function:

(function(){

    //Do Stuff

})()
like image 561
Iladarsda Avatar asked Nov 17 '11 09:11

Iladarsda


People also ask

Can you use an anonymous function in a callback?

In JavaScript, callbacks and anonymous functions can be used interchangeably.

What are self executing functions JavaScript?

The self-executing anonymous function is a special function which is invoked right after it is defined. There is no need to call this function anywhere in the script. This type of function has no name and hence it is called an anonymous function. The function has a trailing set of parenthesis.

Does JavaScript support anonymous function?

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.

What is anonymous self invoking function JavaScript?

A JavaScript function that runs as soon as it is defined. Also known as an IIFE (Immediately Invoked Function Expression).


2 Answers

Of course you can - this is common way of enclosing your variables within some function so they do not interfere with global variables (or from separate closures).

Some example:

(function(){

    var counter = 0;
    var step = function(){
        counter++;
        console.log(counter + ' Mississipi...');
    };

    setInterval(step, 1000);

})();

(function(){

    var counter = 0;
    var step = function(){
        counter++;
        console.log('3 seconds passed for a ' + counter + ' time');
    };

    setInterval(step, 3000);

})();

Thanks to the closures, the variables from them are not interfering with the ones from different closure (different anonymous function).

Working example in this jsfiddle.

EDIT:

Is you want to execute the code from some callback in such function, you may write something like that:

var my_own_callback = function(data){
    // some code for callback working on data passed
};
// ...
(function(callback){
    var result; // future data sent to callback
    // do whatever you need here
    callback(result);
})(my_own_callback);

or even like that:

(function(callback){
    var result; // data that will be sent to callback
    // do whatever you need here
    callback(result);
})(function(data){
    // code for callback using data set to this callback
});

which, however, seems to be rather careless and unnecessarily increasing the complexity of your code.

like image 61
Tadeck Avatar answered Sep 17 '22 15:09

Tadeck


Something like that?

(function(callback){

  //Do Stuff

  //Callback
  if(typeof callback === 'function') {
      callback();
  }
})(myCallback);
like image 36
mamoo Avatar answered Sep 17 '22 15:09

mamoo