How to declare global variables when using the strict mode pragma

前端 未结 5 1501
梦如初夏
梦如初夏 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:22

    I know this is an old question but there's one not mentioned way of getting global context:

    (function(globals){
      "use strict";
      globals.GLOB = {};
    }( (1,eval)('this') ));
    

    (1,eval)('this')

    will evaluate this from global context, so you can paste this wherever you like and you will always get global context

提交回复
热议问题