gpt4 book ai didi

javascript - 为什么 var x = x = x || {} 比 var x = x || 更彻底{}?

转载 作者:IT王子 更新时间:2023-10-29 03:13:39 25 4
gpt4 key购买 nike

在我作为初学者努力编写干净的 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 myApplicationif(!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 年,您可能不想使用其中任何一个。相反,使用适当的模块,通过各种模块定义语法之一( AMDCommonJSRequireJS )与相关工具,或通过 ES2015+ 模块与 importexport (可能还有相关工具,例如 BabelWebpackBrowserify ,尽管当前版本的 Chrome、Safari 和 Edge native 支持模块,而 Firefox 目前也支持一个标志)。

Why is var x = x = x || {} more thorough than var x = x || {}?

不是。

Option 2 is fine but seems to lack a var myApplication beforehand or a if(!window.myApplication) otherwise if myApplication is not in the global scope the condition if(!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, with myApplication in the global scope (due to the var 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 || {}

这是事情发生的顺序:

  1. var myApplication 如果全局不存在则创建它,如果存在则保持不变
  2. 我的应用程序 || {} 被评估并采用 myApplication 中的值(如果为真)或 {}(如果不是);我们称它为 value1
  3. myApplication = value1(中间那个)执行,结果为value1
  4. myApplication = value1(左边那个)无故再次执行

Option 4 in my eyes would have been better written window.myApplication || (myApplication = {}) to avoid throwing an error if myApplication is not in the global scope.

确实。

Option 5 is excluding the false-y values other than undefined but is it a good idea? If myApplication 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com