Javascript immediately invoked function patterns

后端 未结 5 384
既然无缘
既然无缘 2020-12-08 05:34

What do you call these patterns? What is the difference between them? When would you use each? Are there any other similar patterns?

(function() {
    con         


        
5条回答
  •  無奈伤痛
    2020-12-08 06:31

    (function() { 'use strict'; you can use this type

    Why?: An IIFE-Immediately Invoked Function Expression removes variables from the global scope. This helps prevent variables and function declarations from living longer than expected in the global scope, which also helps avoid variable collisions.

    Why?: When your code is minified and bundled into a single file for deployment to a production server, you could have collisions of variables and many global variables. An IIFE protects you against both of these by providing variable scope for each file.

提交回复
热议问题