- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - phát hiện rò rỉ bộ nhớ 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
hoặc if(!window.myApplication)
otherwise if myApplication
不在全局范围内条件 if(!myApplication)
会引发错误,不是吗?
选项 3 是我遇到的问题:我的理解是 myApplication = myApplication
首先执行,myApplication
在全局范围(由于开头的 của chúng tôi
)。我的问题是我想不出这个选项比选项 1 更有效的情况。
选项 4 在我看来,写成 window.myApplication || 会更好(myApplication = {})
以避免在 myApplication
不在全局范围内时引发错误。
选项 5 排除了 không xác định
以外的 false-y 值,但这是个好主意吗?如果 myApplication
是空字符串,则其余代码可能会失败,不是吗?
是否有人能够阐明不同选项之间的差异,特别是解释为什么选项 3 被描述为更彻底?
câu trả lời hay nhất
如果文章声称选项 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+ 模块与 nhập khẩu
Và xuất khẩu
(可能还有相关工具,例如 Babel Và Webpack hoặc 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 thecủa chúng tôi
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
中的值(如果为真)或 {}
(如果不是);我们称它为 giá trị1
myApplication = value1
(中间那个)执行,结果为giá trị1
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
không xác định
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/
这个问题已经有答案了: Can I bind an array to an IN() condition in a PDO query? (23 个回答) 已关闭 5 年前。 任何人都可以看到我如何在
我阅读了关于此的 bash 手册页,但我不明白其中的区别。我对它们进行了测试,它们似乎产生了完全相同的结果。 如果值不是通过命令行参数设置的,我想设置一个变量的默认值。 #!/bin/bash var
我为我的网站开了一家商店,并让它运行起来,但我意识到它无法正确购买商品。换句话说,您不会走进一家商店,拿起一件商品,购买,再次购买,购买,再次购买,等等,以获得想要的数量。你一次捕获他们。我的网站缺少
基本上,我想知道为什么会这样(将列表的内存地址作为参数传递): void init_lista (elemPtr *list) { *list = NULL; } int main(){
看到这个问题:Is there a (built-in) way in JavaScript to check if a string is a valid number?还有这个:jsperf ,其
我有以下字符串: 我想用正则表达式替换所有后面有 px 的数字,并用 X 乘以它们的值。 (X 是一个变量)。 所以如果X=3,结果就是 请注意 X 必须是我将检索到函数的变量 最佳答案 以下代码
这个问题在这里已经有了答案: 关闭 13 年前。 同时 (var != var) System.out.println("循环.."); 执行它..如何声明..var
我只是好奇。我想知道表达式是否有特定原因 var &= expr 行为方式与不同 var = var && expr. 看起来第一个表达式中的表达式正在执行,而不管 var 上的 false 值。 我
我有这个 Ruby 代码: var1 = 10 puts var1.object_id var1 = var1 + 0 puts var1.object_id var1 = var1 + 1 puts
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: demote boost::function to a plain function pointer 所以我
好吧,堆栈溢出, 我花了几个小时来解决 javascript 中的问题(在 NodeJS 服务器中编写),但我真的不明白。 这就是发生的事情: var data = {x: 50}; var temp
首先,我在这里处理 1 和 0,我很清楚 1 == true 和 0 == false。但是我很好奇为什么这不起作用: $var = 1; echo $var; /* 1 */ $var = $var
标题说的是什么:将变量封装在 {}、"" 或 "{}"中是什么意思?我无法在网上找到关于此的任何解释 - 除了使用不会产生任何结果的符号外,我无法引用它们。 这是一个例子: declare -a gr
我需要将信息发送到我的 Html。例如 $(document).ready(function() { var = "'#"+result.tag+"'" // var = '#tag_dinamy
是否可能,如果可以,如何将以下表达式转换为一行? DEV=$(lsblk -no KNAME,MODEL | grep 'ModelNAME') DEV=${DEV%%'ModelNAME'} 简单的
isset($var) == "Test" 和 isset($var) && $var == 'Test" 有什么区别? 最佳答案 这里是一个简短的例子: $var = "Chuck Test"; v
isset($var) == "Test" 和 isset($var) && $var == 'Test" 有什么区别? 最佳答案 这里是一个简短的例子: $var = "Chuck Test"; v
如果我有一个字符串:[Object a:var b:var c:var]; 如何将 a:、b: 和 c: 与正则表达式匹配? 我试过:\[.+\s+(.+:).+\] 但它不适用于 a、b 和 c,它
这个问题在这里已经有了答案: Why does this if-statement combining assignment and an equality check return true? (
我正在 Powershell 中使用 SCSM,但遇到了 if 语句的问题。 我有一个函数,它根据作为变量传递给函数的条件收集数据。 例子: $JMLs1 = collectTickets -crit
Tôi là một lập trình viên xuất sắc, rất giỏi!