What is the purpose of a self executing function in javascript?

前端 未结 19 2853
清酒与你
清酒与你 2020-11-21 04:22

In javascript, when would you want to use this:

(function(){
    //Bunch of code...
})();

over this:

//Bunch of code...


        
相关标签:
19条回答
  • 2020-11-21 04:46

    Here's a solid example of how a self invoking anonymous function could be useful.

    for( var i = 0; i < 10; i++ ) {
      setTimeout(function(){
        console.log(i)
      })
    }
    

    Output: 10, 10, 10, 10, 10...

    for( var i = 0; i < 10; i++ ) {
      (function(num){
        setTimeout(function(){
          console.log(num)
        })
      })(i)
    }
    

    Output: 0, 1, 2, 3, 4...

    0 讨论(0)
  • 2020-11-21 04:46

    Self invoked function in javascript:

    A self-invoking expression is invoked (started) automatically, without being called. A self-invoking expression is invoked right after its created. This is basically used for avoiding naming conflict as well as for achieving encapsulation. The variables or declared objects are not accessible outside this function. For avoiding the problems of minimization(filename.min) always use self executed function.

    0 讨论(0)
  • 2020-11-21 04:48

    I can't believe none of the answers mention implied globals.

    The (function(){})() construct does not protect against implied globals, which to me is the bigger concern, see http://yuiblog.com/blog/2006/06/01/global-domination/

    Basically the function block makes sure all the dependent "global vars" you defined are confined to your program, it does not protect you against defining implicit globals. JSHint or the like can provide recommendations on how to defend against this behavior.

    The more concise var App = {} syntax provides a similar level of protection, and may be wrapped in the function block when on 'public' pages. (see Ember.js or SproutCore for real world examples of libraries that use this construct)

    As far as private properties go, they are kind of overrated unless you are creating a public framework or library, but if you need to implement them, Douglas Crockford has some good ideas.

    0 讨论(0)
  • 2020-11-21 04:48

    Short answer is : to prevent pollution of the Global (or higher) scope.

    IIFE (Immediately Invoked Function Expressions) is the best practice for writing scripts as plug-ins, add-ons, user scripts or whatever scripts are expected to work with other people's scripts. This ensures that any variable you define does not give undesired effects on other scripts.

    This is the other way to write IIFE expression. I personally prefer this following method:

    void function() {
      console.log('boo!');
      // expected output: "boo!"
    }();
    

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void

    From the example above it is very clear that IIFE can also affect efficiency and performance, because the function that is expected to be run only once will be executed once and then dumped into the void for good. This means that function or method declaration does not remain in memory.

    0 讨论(0)
  • 2020-11-21 04:49

    Simplistic. So very normal looking, its almost comforting:

    var userName = "Sean";
    
    console.log(name());
    
    function name() {
      return userName;
    }
    

    However, what if I include a really handy javascript library to my page that translates advanced characters into their base level representations?

    Wait... what?

    I mean, if someone types in a character with some kind of accent on it, but I only want 'English' characters A-Z in my program? Well... the Spanish 'ñ' and French 'é' characters can be translated into base characters of 'n' and 'e'.

    So someone nice person has written a comprehensive character converter out there that I can include in my site... I include it.

    One problem: it has a function in it called 'name' same as my function.

    This is what's called collision. We've got two functions declared in the same scope with the same name. We want to avoid this.

    So we need to scope our code somehow.

    The only way to scope code in javascript is to wrap it in a function:

    function main() {
      // We are now in our own sound-proofed room and the 
      // character-converter libarary's name() function can exist at the 
      // same time as ours. 
    
      var userName = "Sean";
    
      console.log(name());
    
      function name() {
        return userName;
      }
    }
    

    That might solve our problem. Everything is now enclosed and can only be accessed from within our opening and closing braces.

    We have a function in a function... which is weird to look at, but totally legal.

    Only one problem. Our code doesn't work. Our userName variable is never echoed into the console!

    We can solve this issue by adding a call to our function after our existing code block...

    function main() {
      // We are now in our own sound-proofed room and the 
      // character-converter libarary's name() function can exist at the 
      // same time as ours. 
    
      var userName = "Sean";
    
      console.log(name());
    
      function name() {
        return userName;
      }
    }
    
    main();
    

    Or before!

    main();
    
    function main() {
      // We are now in our own sound-proofed room and the 
      // character-converter libarary's name() function can exist at the 
      // same time as ours. 
    
      var userName = "Sean";
    
      console.log(name());
    
      function name() {
        return userName;
      }
    }
    

    A secondary concern: What are the chances that the name 'main' hasn't been used yet? ...so very, very slim.

    We need MORE scoping. And some way to automatically execute our main() function.

    Now we come to auto-execution functions (or self-executing, self-running, whatever).

    ((){})();
    

    The syntax is awkward as sin. However, it works.

    When you wrap a function definition in parentheses, and include a parameter list (another set or parentheses!) it acts as a function call.

    So lets look at our code again, with some self-executing syntax:

    (function main() {
      var userName = "Sean";
    
        console.log(name());
    
        function name() {
          return userName;
        }
      }
    )();
    

    So, in most tutorials you read, you will now be bombard with the term 'anonymous self-executing' or something similar.

    After many years of professional development, I strongly urge you to name every function you write for debugging purposes.

    When something goes wrong (and it will), you will be checking the backtrace in your browser. It is always easier to narrow your code issues when the entries in the stack trace have names!

    Hugely long-winded and I hope it helps!

    0 讨论(0)
  • 2020-11-21 04:49

    Given your simple question: "In javascript, when would you want to use this:..."

    I like @ken_browning and @sean_holding's answers, but here's another use-case that I don't see mentioned:

    let red_tree = new Node(10);
    
    (async function () {
        for (let i = 0; i < 1000; i++) {
            await red_tree.insert(i);
        }
    })();
    
    console.log('----->red_tree.printInOrder():', red_tree.printInOrder());
    

    where Node.insert is some asynchronous action.

    I can't just call await without the async keyword at the declaration of my function, and i don't need a named function for later use, but need to await that insert call or i need some other richer features (who knows?).

    0 讨论(0)
提交回复
热议问题