最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript - self-executing anonymous functions and callback - Stack Overflow

programmeradmin0浏览0评论

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

JavaScript self-executing function:

(function(){

    //Do Stuff

})()

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

JavaScript self-executing function:

(function(){

    //Do Stuff

})()
Share Improve this question asked Nov 17, 2011 at 9:42 IladarsdaIladarsda 10.7k40 gold badges107 silver badges171 bronze badges 8
  • 1 Plz clarify, what do you mean by "using callback"? – alex vasi Commented Nov 17, 2011 at 9:46
  • 1 @alex vasi - code to be executed once everything within the self-executing function is done. – Iladarsda 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? – Tadeck 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. – Iladarsda 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. – Tadeck Commented Nov 17, 2011 at 9:59
 |  Show 3 more comments

2 Answers 2

Reset to default 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.

Something like that?

(function(callback){

  //Do Stuff

  //Callback
  if(typeof callback === 'function') {
      callback();
  }
})(myCallback);
发布评论

评论列表(0)

  1. 暂无评论