JavaScript global variables & self-invoking anonymous functions

后端 未结 4 2156
走了就别回头了
走了就别回头了 2021-02-15 16:24

So I\'ve been reading through Javascript - The Good Parts and one thing that Crockford points out is the use weakness of global variables in Javascript, in such a way that if yo

4条回答
  •  温柔的废话
    2021-02-15 17:29

    (function (global) {
        global.x = 'meh';
    })(window);
    (function () {
        alert(typeof x); // string
    })();
    

提交回复
热议问题