- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在我作为初学者努力编写干净的 Javascript 代码时,我最近阅读了 this article当我偶然发现这一段时,关于 JavaScript 中的命名空间:
The code at the very top of the next sample demonstrates the different ways in which you can check to see if a variable (object namespace) already exists before defining it. You'll commonly see developers using Option 1, however Options 3 and 5 may be considered more thorough and Option 4 is considered a good best-practice.
// This doesn't check for existence of 'myApplication' in
// the global namespace. Bad practice as you can easily
// clobber an existing variable/namespace with the same name
var myApplication = {};
/*
The following options *do* check for variable/namespace existence.
If already defined, we use that instance, otherwise we assign a new
object literal to myApplication.
Option 1: var myApplication = myApplication || {};
Option 2 if(!MyApplication) MyApplication = {};
Option 3: var myApplication = myApplication = myApplication || {}
Option 4: myApplication || (myApplication = {});
Option 5: var myApplication = myApplication === undefined ? {} : myApplication;
*/
选项 1 当然是我见过最常使用的选项,我也很理解。
选项 2 很好,但似乎缺少 var myApplication
或 if(!window.myApplication)
otherwise if myApplication
不在全局范围内条件 if(!myApplication)
会引发错误,不是吗?
选项 3 是我遇到的问题:我的理解是 myApplication = myApplication
首先执行,myApplication
在全局范围(由于开头的 var
)。我的问题是我想不出这个选项比选项 1 更有效的情况。
选项 4 在我看来,写成 window.myApplication || 会更好(myApplication = {})
以避免在 myApplication
不在全局范围内时引发错误。
选项 5 排除了 undefined
以外的 false-y 值,但这是个好主意吗?如果 myApplication
是空字符串,则其余代码可能会失败,不是吗?
是否有人能够阐明不同选项之间的差异,特别是解释为什么选项 3 被描述为更彻底?
最佳答案
如果文章声称选项 3“更彻底”,那是不正确的。该分配链的中间根本没有意义。
Would someone be able to shed some light on the differences between the different options and in particular explain why option 3 is described as more thorough?
首先,一个警告:在 2018 年,您可能不想使用其中任何一个。相反,使用适当的模块,通过各种模块定义语法之一( AMD , CommonJS , RequireJS )与相关工具,或通过 ES2015+ 模块与 import
和 export
(可能还有相关工具,例如 Babel 和 Webpack 或 Browserify ,尽管当前版本的 Chrome、Safari 和 Edge native 支持模块,而 Firefox 目前也支持一个标志)。
Why is
var x = x = x || {}
more thorough thanvar x = x || {}
?
不是。
Option 2 is fine but seems to lack a
var myApplication
beforehand or aif(!window.myApplication)
otherwise ifmyApplication
is not in the global scope the conditionif(!myApplication)
would throw an error, wouldn't it?
是的。 (假设生产发生在全局范围内。如果它不在全局范围内并且在当前范围链中的任何地方都有一个范围内的myApplication
,它不会抛出因为myApplication
不会是未解析的符号。)
Option 3 is the one I have trouble with: my understanding is that the
myApplication = myApplication
is executed first, withmyApplication
in the global scope (due to thevar
at the begining). My problem is that I can't think of a case where this option does anything more than option 1.
没有,如果你有
var myApplication = myApplication = myApplication || {}
这是事情发生的顺序:
var myApplication
如果全局不存在则创建它,如果存在则保持不变我的应用程序 || {}
被评估并采用 myApplication
中的值(如果为真)或 {}
(如果不是);我们称它为 value1
myApplication = value1
(中间那个)执行,结果为value1
myApplication = value1
(左边那个)无故再次执行Option 4 in my eyes would have been better written
window.myApplication || (myApplication = {})
to avoid throwing an error ifmyApplication
is not in the global scope.
确实。
Option 5 is excluding the false-y values other than
undefined
but is it a good idea? IfmyApplication
is say an empty string, the rest of the code is likely to fail, isn't it?
是的。
关于javascript - 为什么 var x = x = x || {} 比 var x = x || 更彻底{}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49852334/
我是一名优秀的程序员,十分优秀!