javascript 逻辑赋值是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4446433/
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
How does javascript logical assignment work?
提问by Incognito
In javascript, if we have some code such as
在 javascript 中,如果我们有一些代码,例如
var a = "one";
var b = q || a;
alert (b);
The logical OR operator will assign a's value to b, and the alert will be "one."
逻辑 OR 运算符会将 a 的值分配给 b,并且警报将为“一”。
Is this limited to assignments only or can we use it everywhere?
这仅限于分配还是我们可以在任何地方使用它?
It seems an empty string is treated the same as undefined. Is this right?
似乎空字符串被视为未定义。这是正确的吗?
How does this work with AND variables? What about combinations of them?
这如何与 AND 变量一起工作?它们的组合呢?
What is a good example of when to use these idioms, or when not to?
什么时候使用这些习语,什么时候不使用的好例子是什么?
回答by dheerosaur
For your q || ato evaluate to a, qshould be a 'falsy' value. What you did is called "Short circuit evaluation".
对于您q || a的评估a,q应该是一个“假”值。您所做的称为“短路评估”。
Answering your questions:
回答您的问题:
The logical operators (like and -
&&, or -||) can be used in other situations too. More generally in conditional statements likeif. More hereEmpty string is not treated as
undefined. Both are falsy values. There are a few more falsy values. More hereAND, or&&in JavaScript, is not a variable. It is an operatorThe idiom you have used is quite common.
var x = val || 'default'; //is generally a replacement forvar x = val ? val : 'default' //orif (val) var x = val; else var x = 'default';
回答by Anon.
The way ||works in Javascript is:
||在 Javascript 中的工作方式是:
- If the left operand evaluates as 
true, return the left operand - Otherwise, return the right operand
 
- 如果左操作数计算为
true,则返回左操作数 - 否则,返回正确的操作数
 
&& works similarly.
&& 的工作原理类似。
You can make use of this for in-line existence checks, for example:
您可以将其用于内联存在检查,例如:
var foo = (obj && obj.property)
will set footo obj.propertyif objis defined and "truthy".
将设置foo为obj.propertyifobj已定义且“真实”。
回答by Kim Burgaard
This behavior is shared with other scripting languages like Perl. The logical OR operator can be used as a syntactic shorthand for specifying default values due to the fact that the logical OR operator stops evaluating its operands when it encounters the first expression that evaluates to true: "evaluate the first operand and if the value is interpreted as not false, assign it. Otherwise repeat for the second operand."
此行为与 Perl 等其他脚本语言共享。逻辑 OR 运算符可用作指定默认值的语法速记,因为当逻辑 OR 运算符遇到第一个计算结果为真的表达式时,它会停止计算其操作数:“计算第一个操作数,如果该值被解释不为假,分配它。否则对第二个操作数重复。”
I find I often use this behavior to specify default values for function parameters. E.g.
我发现我经常使用这种行为来指定函数参数的默认值。例如
function myFunction(foo, bar) {
    var fooValue = foo || "a";
    // no need to test fooValue -- it's guaranteed to be defined at this point
}
回答by Robert
I'm not quite sure I follow your question. You can use an expression anywhere you can use an expression, and a logical operator on two expressions results in an expression.
我不太确定我是否遵循了您的问题。您可以在任何可以使用表达式的地方使用表达式,两个表达式上的逻辑运算符会生成一个表达式。
alert(q||a);
alert(true||false);
var x=5;
var y=0;
if (y!=0 && x/y>2) { /*do something*/ }
The last bit is useful. Like most languages, Javascript 'short-circuits' ANDs and ORs. If the first part of an AND is false, it doesn't evaluate the second bit - saving you a divide-by-0. If the first part of an OR is true, it doesn't evaluate the second.
最后一点很有用。像大多数语言一样,Javascript“短路”AND 和 OR。如果 AND 的第一部分为假,则不会评估第二位 - 为您节省除以 0。如果 OR 的第一部分为真,则不会评估第二部分。
But you can use boolean operators anywhere you can use an expression.
但是您可以在任何可以使用表达式的地方使用布尔运算符。
回答by gianebao
Javascript evaluates logic by truethness/falseness. Values such as (false, "", null, undefined, 0, -0) are evaluated as logic false.
Javascript 通过真假来评估逻辑。( false, "", null, undefined, 0, -0) 等值被评估为逻辑假。
Combine this with the lazy evaluation, 'OR' operations are now evaluated from left to rightand stopsonce trueis found. Since, in your example, the truthness is not literally a boolean, the value is returned.
与懒惰的评价结合这“或”现在的操作是由评估left to right和停止一次true被发现。由于在您的示例中,真实性不是字面上的布尔值,因此返回该值。
In this case:
在这种情况下:
x = 0; y = 5; alert(y || x)/*returns 5*/;  alert(x || y)/*also returns 5*/;
this can also be other objects.
这也可以是其他对象。
functionThatReturnsTrue() || functionThatDoesSomething();
回答by Joe Kuan
IMHO - don't use for boolean type assignment. It can be confusing. As undefined !== false, ie false itself is a value.
恕我直言 - 不要用于布尔类型分配。它可能会令人困惑。由于 undefined !== false,即 false 本身就是一个值。
E.g. If u want to copy a field value from an object if and only if that field is defined
例如,如果您想从对象复制字段值,当且仅当该字段已定义
var bar.value = false;
var foo = true;
var foo = bar.value || foo;  // ==> should be false as bar.value is defined
For boolean type assignment, u should really use
对于布尔类型赋值,你应该真正使用
var foo = (bar.value !== undefined) ? bar.value : foo;

