使用 || 的这段代码的确切含义是什么? (“OR”)运算符?
我在某处看到了这个代码片段:
var idx = SOME_VALUE;
var color = {
yellor: 1,
red: 2,
black: 0
};
var x = color[idx] || []; // Is this means if color[idx] is null, then return an empty array?
我只能猜测代码 var x = color[idx] || [];
表示如果 color[idx]
为 null,则将空数组返回给 x,否则 x= color[idx]。我说得对吗?
尽管如此,我还是需要一个解释。这段代码和下面的逻辑是不是一样?
CONDITION==VALUE? TRUE_goes_here : FALSE_goes_here
I saw somewhere this code snippet:
var idx = SOME_VALUE;
var color = {
yellor: 1,
red: 2,
black: 0
};
var x = color[idx] || []; // Is this means if color[idx] is null, then return an empty array?
I can only guess the code var x = color[idx] || [];
means if color[idx]
is null, then return an empty array to x otherwise x= color[idx]. Am I right?
Still, I need an explaination. Does this code has the same logic as the following?
CONDITION==VALUE? TRUE_goes_here : FALSE_goes_here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这意味着如果
color[idx]
为“falsy”,则使用空数组。 “Falsy”值为false
(当然)、0
、NaN
、""
、undefined
和null
(所有其他值均为“true”)。这个习惯用法是 JavaScript 异常强大的|| 的一个例子。
运算符*。在这种情况下,如果
color
不包含名称由idx
包含的属性(因为当您索引到这样的对象并且键与任何现有属性名称不匹配,结果为undefined
):x
将是1
(如果idx
是“yellor”),2
(如果idx
为“红色”),0
(如果idx
为“黑色”),或[]
如果 < code>idx 是其他任何东西。因此,在问题结束时回答您的问题,基本上是的。它是:
或
*(这是我贫血的小博客上的一篇文章。)
What it means is that if
color[idx]
is "falsy," use an empty array instead. "Falsy" values arefalse
(of course),0
,NaN
,""
,undefined
, andnull
(all other values are "truthy"). That idiom is an example of JavaScript's curiously powerful||
operator*.In this case, it's supplying a default value if
color
doesn't contain a property with the name contained byidx
(because when you index into an object like that and the key doesn't match any existing property name, the result isundefined
):x
will be1
(ifidx
is "yellor"),2
(ifidx
is "red"),0
(ifidx
is "black"), or[]
ifidx
is anything else.So answering your question at the end of your question, basically, yes. It's:
or
* (That's a post on my anemic little blog.)
你是对的。
在 Javascript 中,
a || b
计算结果为第一个“真实”操作数。如果
a
为“falsy”(例如,null
、false
、undefined
、0
),它将计算为b
。You are correct.
In Javascript,
a || b
evaluates to the first "truthy" operand.If
a
is "falsy" (eg,null
,false
,undefined
,0
), it will evaluate tob
.or 运算符
||
将为您提供第一个不计算为条件的值allfalse
(如0
或null
执行)。这意味着使用
0 ||空|| 5
将产生结果 5。该代码中的内容是 JavaScript 中的常见做法,即使用
||
运算符在变量初始化时指定默认值。那只是因为比
The or operator
||
will give you the first value that doesn't evaluate as conditionallfalse
(like0
ornull
do).This means that using
0 || null || 5
will yield 5 as a result.What you have in that code is a common practice in JavaScript which is using the
||
operator to specify default values upon variable initialization. That's just becauseis a bit more readable than
如果
color
是一个对象,其属性名称为String(idx)
并且该属性的值为 true,则将该属性的值分配给x< /代码>。否则,让
x
为空数组。If
color
is an object with a property which name isString(idx)
and the value of that property is truthy, assign the value of that property tox
. Otherwise, letx
be an empty array.在这种情况下,
||
是“布尔或”运算符。与许多语言一样,JavaScript 中的布尔运算符是短路的。例如,假设我有以下布尔表达式:如果第一个函数返回值与 true 等价,则无需计算第二个值,并且
otherFunction()
将不会执行,因为“or”运算符返回一个真实等价的值,无论其某些操作是否是真实等价的。这很好,因为它使您的代码更加高效:仅计算所需的表达式。此外,在这种情况下还有一个奇怪的行为:由于表达式的第一个值是 true 等价的,JavaScript 只是将其作为表达式的结果返回,因为这意味着整个表达式无论如何都是 true。
在您的代码中,
color[idx]
的值如果已设置,则为 true-equivvalent。由于它是“或”运算中的真等价值,因此它作为表达式的值返回。但是,如果未设置,则返回undefined
,这是一个假等效值。因此 JavaScript 必须计算下一个值来定义“or”表达式是否为真。由于下一个值是最后一个值,并且表达式的值完全取决于它,因此如果第一个值是假等价的,则表达式将返回下一个值。In the case,
||
is the "boolean or" operator. As in many languages, the boolean operators in JavaScript are short-circuited. For example, let us suppose I have the following boolean expression:If the first function returned value is true-equivalent, there is no need to evaluate the second value and the
otherFunction()
will not be executed because the "or" operator returns a true-equivalent value whether some of its operated is true-equivalent. This is good because it makes your code more efficient: only the needed expression is evaluated.Also, there is a curious behavior in this case: since the first value of the expression is true-equivalent, JavaScript just returns it as the result of the expression because that would mean the entire expression is true anyway.
In your code, the value of
color[idx]
is true-equivvalent if it is set. Since it is true-equivalent value in an "or" operation, it is returned as the value of the expression. However, if it not set,undefined
is returned, which is a false-equivalent value. So JavaScript has to evaluate the next value to define if the "or" expression is true. Since the next value is the last one and the value of the expressions depends entirely of it, the expression returns the next value if the first is false-equivalent.