javascript 返回&&

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4490274/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:52:41  来源:igfitidea点击:

returning with &&

javascriptoperatorsreturn

提问by steve

What does it mean to return a value with &&?

用&&​​返回一个值是什么意思?

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, " -" );
    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) ;

回答by dheerosaur

return a && bmeans "return a if a is falsy, return b if a is truthy".

return a && b意思是“如果 a 为假则返回 a,如果 a 为真则返回 b”。

It is equivalent to

它相当于

if (a) return b;
else return a;

回答by Spiny Norman

The logical AND operator, &&, works similarly. If the first object is falsy, it returns that object. If it is truthy, it returns the second object. (from https://www.nfriedly.com/techblog/2009/07/advanced-javascript-operators-and-truthy-falsy/).

逻辑 AND 运算符 && 的工作原理类似。如果第一个对象为假,则返回该对象。如果为真,则返回第二个对象。(来自https://www.nfriedly.com/techblog/2009/07/advanced-javascript-operators-and-truthy-falsy/)。

Interesting stuff!

有趣的东西!

EDIT: So, in your case, if document.defaultView.getComputedStyle(elem, " ")does not return a meaningful ("truthy") value, that value is returned. Otherwise, it returns s.getPropertyValue(name).

编辑:因此,在您的情况下,如果document.defaultView.getComputedStyle(elem, " ")没有返回有意义的(“真实”)值,则返回该值。否则,它返回s.getPropertyValue(name)

回答by S7_0

The AND && operator does the following:

AND && 运算符执行以下操作:

  • Evaluate operands from left to right.
  • For each operand, convert it to a boolean. If the resultis false, stop and return the original value of that result.
  • If all other operands have been assessed (i.e. all were truthy), return the last operand.
  • 从左到右计算操作数。
  • 对于每个操作数,将其转换为布尔值。如果结果为假,则停止并返回该结果的原始值。
  • 如果所有其他操作数都已评估(即全部为真),则返回最后一个操作数。

As I said, each operand is convert to a boolean, if it's 0 it's falsyand every other value different than 0 (1, 56, -2, etc etc) are truthy

正如我所说,每个操作数都被转换为一个布尔值,如果它是 0 它是假的,并且每个其他不同于 0 的值(1、56、-2 等)都是

In other words, AND returns the first falsy value or the last value if none were found.

换句话说,如果没有找到,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

We can also pass several values in a row. See how the first falsy one is returned:

我们还可以连续传递多个值。看看第一个假是如何返回的:

return 1 && 2 && null && 3 // null

When all values are truthy, the last value is returned:

当所有值都为真时,返回最后一个值:

return 1 && 2 && 3 // 3, the last one

You can learn more about the logical operator here https://javascript.info/logical-operators

您可以在此处了解有关逻辑运算符的更多信息https://javascript.info/logical-operators