What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?

后端 未结 8 1687
轮回少年
轮回少年 2020-11-22 02:02

I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported.

(f         


        
8条回答
  •  爱一瞬间的悲伤
    2020-11-22 02:37

    We should also use 'use strict' in the scope function to make sure that the code should be executed in "strict mode". Sample code shown below

    (function() {
        'use strict';
    
        //Your code from here
    })();
    

提交回复
热议问题