12

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

JavaScript self-executing function:

(function(){

    //Do Stuff

})()
8
  • 1
    Plz clarify, what do you mean by "using callback"? Commented Nov 17, 2011 at 9:46
  • 1
    @alex vasi - code to be executed once everything within the self-executing function is done. Commented Nov 17, 2011 at 9:48
  • 1
    @NewUser: Then you would need to define " done ". Does it mean that if you attach some events, they need to be called at least once? Or that it will be impossible to call them again? Commented Nov 17, 2011 at 9:52
  • @Tadeck - done as all the code within self-executing function has been executed, I will be run only once - one page / script load. Commented Nov 17, 2011 at 9:55
  • @NewUser: I follow, but this is more complex. You can write some code within this anonymous function, but it does not mean it will be executed instantly. As in event-based programming, some code may wait until it is triggered by some event (such as user clicking on something). But I will update my answer to suit what I think you want. Commented Nov 17, 2011 at 9:59

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, as callback I meant code to be executed once everything within the self-executing function is done
@NewUser as long as you don't have asynchronous code in the self-executing function, the rest of the code will execute sequentially (meaning, there is no need to use a callback, just continue with your code). If you have asynchronous code, you'll have to transport your callback to those code-blocks and handle it appropriately.
@Yoski - are you sure that code in anonymous function is execute sequentially? Is there any way we can actually check that?
@NewUser: Unless you execute some code asynchronously, it is executed synchronously. But if you define some callbacks and pass them eg. as event handlers, then do not expect them to be executed before the main function ends - they are just passed and may be executed any time event occurres. But yes, generally the code is executed line-by-line. You can check it by freezing your browser when you include indefinite for loop in your code.
@NewUser: No, the second case I can think of is callbacks - they are executed whenever they are called by the function you pass it to. So, if you assign some callback to some event eg. using jQuery, then these specific callbacks will not be executed unless they will be called by some event. Is it clear enough? If you use asynchronous code or pass some callbacks to some code outside your closure, you do not have guarantee that every single line in your closure is executed at least once. Is it clear?
|
3

Something like that?

(function(callback){

  //Do Stuff

  //Callback
  if(typeof callback === 'function') {
      callback();
  }
})(myCallback);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.