JavaScript 变量回退
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9586278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript Variable fallback
提问by thugsb
Please can someone explain to me what this line of code does:
请有人向我解释这行代码的作用:
var list = calls[ev] || (calls[ev] = {});
My best guess:
我最好的猜测:
It's setting the variable "list" with the value of calls.xxx, where xxx is a variable, ev. If calls[ev] doesn't exist, then it's creating it as an empty object and assigning that empty object to "list". Is that right?
它使用calls.xxx 的值设置变量“list”,其中xxx 是一个变量ev。如果 call[ev] 不存在,那么它会将它创建为一个空对象并将该空对象分配给“列表”。是对的吗?
Why are the parenthesis being used? Where can I find out more info on using || when setting variables, and the use of parenthesis in this context? Thanks!
为什么要使用括号?我在哪里可以找到有关使用 || 的更多信息 设置变量时,以及在这种情况下使用括号?谢谢!
回答by Alexander Pavlov
This code is equivalent to
这段代码相当于
var list;
if (calls[ev])
list = calls[ev];
else {
calls[ev] = {};
list = calls[ev];
}
Two features of the language are used:
使用了该语言的两个特性:
- The shortcutcomputation of boolean expressions (consider
a || b
. Ifa
istrue
thenb
is not evaluated). Thus, if you assignvar v = a || b;
anda
evaluates to something that can be cast totrue
, thenb
is not evaluated. - The assignment statement evaluates to the last assigned value (to enable
var a = b = c;
)
- 布尔表达式的快捷计算(考虑
a || b
. Ifa
istrue
thenb
不计算)。因此,如果您对可以转换为 的内容进行分配var v = a || b;
和a
评估true
,则b
不会对其进行评估。 - 赋值语句计算为最后一个赋值(以启用
var a = b = c;
)
The parentheses are necessary to avoid this interpretation:
括号是必要的,以避免这种解释:
var list = (calls[ev] || calls[ev]) = {};
(which is an error).
(这是一个错误)。
回答by Alex Turpin
Your guess is right. This is a common way to declare "default" values for variables in JavaScript.
你的猜测是对的。这是在 JavaScript 中为变量声明“默认”值的常用方法。
function foo(bar) {
var bar = bar || 0; //This sets bar to 0 if it's not already set
console.log(bar);
}
The way this works is that in JavaScript, an undefined variable is falsy, meaning that in any boolean comparaison operation, it will evaluate to false
. You can then use the OR operator to combine two values and it will return the first value that evaluates to true
.
它的工作方式是在 JavaScript 中,未定义的变量是假的,这意味着在任何布尔比较运算中,它的计算结果为false
。然后,您可以使用 OR 运算符组合两个值,它将返回计算结果为 的第一个值true
。
回答by Paul Dixon
||
or 'logical OR' has a higher precedencethan the assignment operator =
, thus the parentheses are necessary to ensure this idiom evaluates in the right order
||
或 'logical OR' 的优先级高于赋值运算符=
,因此括号是必要的,以确保此习语以正确的顺序计算
The other thing to be aware of is that many languages, Javascript included, provide short-circuit evaluationof boolean operators like AND and OR. If the first operand of a logical-or evaluates true, there is no need to evaluate the second operand, as it would make no difference to the result.
另一件需要注意的事情是,包括 Javascript 在内的许多语言都提供了对 AND 和 OR 等布尔运算符的短路评估。如果逻辑或的第一个操作数计算为真,则无需计算第二个操作数,因为它不会对结果产生影响。
Understand this, and you'll see this isn't some special assignment syntax, but an idiom, or pattern, that exploits a language feature to provide a more compact representation of an idea.
理解这一点,您就会发现这不是一些特殊的赋值语法,而是一种习惯用法或模式,它利用语言功能来提供更紧凑的想法表示。
回答by joidegn
you are right about your first guess. This is a common pattern for initialising javascript namespaces. It serves to make sure you dont overwrite a previous object with the same name. Most popular libraries will do something similar in order to create their namespace objects.
你的第一个猜测是对的。这是初始化 javascript 命名空间的常见模式。它用于确保您不会覆盖具有相同名称的先前对象。大多数流行的库都会做类似的事情来创建它们的命名空间对象。
The parenthesis is there so that the expressions are evaluated in the proper order.
括号在那里,以便以正确的顺序评估表达式。