[removed] global scope

前端 未结 4 650
一个人的身影
一个人的身影 2021-02-03 12:15

Nowdays, i create a .js file with a lot of functions and then I link it to my html pages. That\'s working but I want to know what\'s the best way (good practices) to insert js i

4条回答
  •  花落未央
    2021-02-03 12:55

    The best way is to create a new scope and execute your code there.

    (function(){
      //code here
    })();
    

    This is best used when the global scope is accessed at a minimum.

    Basically, this defines an anonymous function, gives it a new scope, and calls it.

提交回复
热议问题