ste*_*eve 27 javascript return operators
用&&返回值是什么意思?
else if (document.defaultView && document.defaultView.getComputedStyle) {
// It uses the traditional ' text-align' style of rule writing,
// instead of textAlign
name = name.replace(/([A-Z]) /g, " -$1" );
name = name.toLowerCase();
// Get the style object and get the value of the property (if it exists)
var s = document.defaultView.getComputedStyle(elem, " ") ;
return s && s.getPropertyValue(name) ;
Run Code Online (Sandbox Code Playgroud)
dhe*_*aur 54
return a && b 意思是"如果a是假的则返回a,如果a是真的则返回b".
它相当于
if (a) return b;
else return a;
Run Code Online (Sandbox Code Playgroud)
Spi*_*man 15
逻辑AND运算符&&的工作方式类似.如果第一个对象是假的,则返回该对象.如果它是真实的,它返回第二个对象.(来自https://www.nfriedly.com/techblog/2009/07/advanced-javascript-operators-and-truthy-falsy/).
有趣的东西!
编辑:因此,在您的情况下,如果document.defaultView.getComputedStyle(elem, " ")没有返回有意义的("真理")值,则返回该值.否则,它返回s.getPropertyValue(name).
S7_*_*7_0 15
AND && 运算符执行以下操作:
正如我所说,每个操作数都被转换为一个布尔值,如果它是 0 它是假的,并且每个其他不同于 0 的值(1、56、-2 等)都是真值
换句话说,如果没有找到,AND 返回第一个假值或最后一个值。
// if the first operand is truthy,
// AND returns the second operand:
return 1 && 0 // 0
return 1 && 5 // 5
// if the first operand is falsy,
// AND returns it. The second operand is ignored
return null && 5 // null
return 0 && "no matter what" // 0
Run Code Online (Sandbox Code Playgroud)
我们还可以连续传递多个值。看看第一个假是如何返回的:
return 1 && 2 && null && 3 // null
Run Code Online (Sandbox Code Playgroud)
当所有值都为真时,返回最后一个值:
return 1 && 2 && 3 // 3, the last one
Run Code Online (Sandbox Code Playgroud)
您可以在此处了解有关逻辑运算符的更多信息https://javascript.info/logical-operators
| 归档时间: |
|
| 查看次数: |
11662 次 |
| 最近记录: |