我正在调试一些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声明。
当前回答
基本上,它检查||之前的值是否为true。如果是,则取该值,如果不是,则取||之后的值。
它将取||之后的值(据我所知):
未定义的 假 0 (Null或空字符串)
其他回答
如果没有设置title,则使用'ERROR'作为默认值。
更通用的:
var foobar = foo || default;
读取:将foobar设置为foo或默认值。 你甚至可以把这个串起来很多次:
var foobar = foo || bar || something || 42;
再解释一下……
||操作符是逻辑或操作符。如果第一部分为真,则结果为真;如果第二部分为真,则结果为真;如果两部分都为真,则结果为真。为了清晰起见,这里是一个表格:
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在布尔上下文中认为什么是真或假。
为了对我前面所说的一切进行解释,我应该给你们一些例子来理解逻辑概念。
var name = false || "Mohsen"; # name equals to Mohsen
var family = true || "Alizadeh" # family equals to true
这意味着如果左边的语句被赋值为true,那么它将被结束,左边的语句将被返回并赋值给变量。在其他情况下,右侧将返回并分配。
和运算符有相反的结构,如下所示。
var name = false && "Mohsen" # name equals to false
var family = true && "Alizadeh" # family equals to Alizadeh
这意味着title参数是可选的。因此,如果你不带参数调用该方法,它将使用默认值“Error”。
它是写作的简写:
if (!title) {
title = "Error";
}
这种使用布尔表达式的简写技巧在Perl中也很常见。用这样的表达式:
a OR b
如果a或b为真,它的值为真。所以如果a为真,你根本不需要检查b。这被称为短路布尔计算,因此:
var title = title || "Error";
基本上检查title的结果是否为false。如果是,则返回"Error",否则返回title。
双管代表逻辑上的“或”。这不是真正的情况下,“参数未设置”,因为严格在JavaScript中,如果你有这样的代码:
function foo(par) {
}
然后调用
foo()
foo("")
foo(null)
foo(undefined)
foo(0)
不相等。
双管道(||)将把第一个参数强制转换为布尔值,如果结果布尔值为真-执行赋值,否则它将赋值正确的部分。
如果你检查未设置参数,这很重要。
我们有一个函数setSalary,它有一个可选参数。如果用户没有提供参数,则应该使用默认值10。
如果你这样检查:
function setSalary(dollars) {
salary = dollars || 10
}
这将为调用提供一个意想不到的结果,例如:
setSalary(0)
它仍然会按照上面描述的流程设置10。