How to declare global variables when using the strict mode pragma

前端 未结 5 1454
梦如初夏
梦如初夏 2021-01-31 16:52

It\'s considered good practice to use a self-invoking function to wrap strict mode compliant code, often called the strict mode pragma:

(function(){
  \"use stri         


        
5条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 17:14

    IMO alternative 3 is best. But it assumes that window represents the global scope - which is true for the browser but not for other JS environments (command line, Node.js, etc.).

    The following will work across the board:

    (function(globals){
      "use strict";
      globals.GLOB = {};
    }(this));
    

提交回复
热议问题