You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The text was updated successfully, but these errors were encountered:
Pomelo1213
changed the title
[Daily Tip翻译]Keeping your JavaScript out of the global scope
[Daily Tips翻译]Keeping your JavaScript out of the global scope
Aug 21, 2018
Source Website
2017年12月15日
Keeping your JavaScript out of the global scope (and why you want to)
这周,我们学习了关于JavaScript Scope和how to define and update variables across different types of scope
你也许想在这时候在全局作用域中声明一个函数或者变量(你想让其他的script能够使用它,例如,一个轻量级的框架。)
但是这样来说,你想在全局作用域中使用你的方法和变量。同时,如果另一个script或者另一个开发者定义了一个和你的script里面名字一模一样的函数或者变量,那将会出现重写或者造成冲突。
你可以在词法作用域中用一个函数封装你的代码。
如果你想当文件运行且没有加载你的函数时代码立即运行,你可以使用一种名为立即执行函数来做这件事情。立即执行函数是(内嵌,没有名字)一个匿名函数可以立刻运行。
使用两种之一可以让你的代码更强壮。
The text was updated successfully, but these errors were encountered: