我正在调试一些JavaScript,无法解释这个||做什么:
function (title, msg) {
var title = title || 'Error';
var msg = msg || 'Error on Request';
}
为什么这个家伙使用var title = title || 'ERROR'?我有时也看到它没有var声明。
我正在调试一些JavaScript,无法解释这个||做什么:
function (title, msg) {
var title = title || 'Error';
var msg = msg || 'Error on Request';
}
为什么这个家伙使用var title = title || 'ERROR'?我有时也看到它没有var声明。
当前回答
||为布尔或运算符。在JavaScript中,undefined, null, 0, false被认为是假值。
它只是意味着
true || true = true
false || true = true
true || false = true
false || false = false
undefined || "value" = "value"
"value" || undefined = "value"
null || "value" = "value"
"value" || null = "value"
0 || "value" = "value"
"value" || 0 = "value"
false || "value" = "value"
"value" || false = "value"
其他回答
虽然Cletus的回答是正确的,但我觉得应该在JavaScript中添加更多关于“评估为假”的细节。
var title = title || 'Error';
var msg = msg || 'Error on Request';
不仅仅是检查是否提供了title/msg,而且还检查它们中的任何一个是假的。即下列其中一项:
假的。 0(零) ""(空字符串) null。 未定义的。 NaN(一个特殊的数字值,意思不是一个数字!)
在这一行中
var title = title || 'Error';
如果title为真(即,不是假的,所以title = "titleMessage"等),那么布尔OR(||)运算符已经找到了一个“真”值,这意味着它的计算结果为真,因此它短路并返回真值(title)。
如果title是假的(即上面的列表之一),那么布尔OR(||)运算符已经找到了一个“假”值,现在需要计算运算符的另一部分“Error”,该运算符的计算结果为真,因此返回。
如果运算符两边的值都为false,它也会返回第二个“falsy”运算符(经过一些快速的firebug控制台实验)。
i.e.
return ("" || undefined)
返回undefined,这可能是为了允许您在尝试将title/message默认为""时使用此问题中询问的行为。也就是跑步之后
var foo = undefined
foo = foo || ""
Foo将被设置为""
||为布尔或运算符。在JavaScript中,undefined, null, 0, false被认为是假值。
它只是意味着
true || true = true
false || true = true
true || false = true
false || false = false
undefined || "value" = "value"
"value" || undefined = "value"
null || "value" = "value"
"value" || null = "value"
0 || "value" = "value"
"value" || 0 = "value"
false || "value" = "value"
"value" || false = "value"
基本上,它检查||之前的值是否为true。如果是,则取该值,如果不是,则取||之后的值。
它将取||之后的值(据我所知):
未定义的 假 0 (Null或空字符串)
双管操作员
这个例子可能有用:
var section = document.getElementById('special');
if(!section){
section = document.getElementById('main');
}
它也可以是:
var section = document.getElementById('special') || document.getElementById('main');
再解释一下……
||操作符是逻辑或操作符。如果第一部分为真,则结果为真;如果第二部分为真,则结果为真;如果两部分都为真,则结果为真。为了清晰起见,这里是一个表格:
X | Y | X || Y
---+---+--------
F | F | F
---+---+--------
F | T | T
---+---+--------
T | F | T
---+---+--------
T | T | T
---+---+--------
Now notice something here? If X is true, the result is always true. So if we know that X is true we don't have to check Y at all. Many languages thus implement "short circuit" evaluators for logical-or (and logical-and coming from the other direction). They check the first element and if that's true they don't bother checking the second at all. The result (in logical terms) is the same, but in terms of execution there's potentially a huge difference if the second element is expensive to calculate.
那么这和你的例子有什么关系呢?
var title = title || 'Error';
Let's look at that. The title element is passed in to your function. In JavaScript if you don't pass in a parameter, it defaults to a null value. Also in JavaScript if your variable is a null value it is considered to be false by the logical operators. So if this function is called with a title given, it is a non-false value and thus assigned to the local variable. If, however, it is not given a value, it is a null value and thus false. The logical-or operator then evaluates the second expression and returns 'Error' instead. So now the local variable is given the value 'Error'.
这是因为在JavaScript中实现了逻辑表达式。它不会返回一个合适的布尔值(真或假),而是返回它在一些规则下给出的值,这些规则被认为是等价于真,什么被认为是等价于假。查看JavaScript参考,了解JavaScript在布尔上下文中认为什么是真或假。