Javascript 中的空合并?

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

Null-coalescing in Javascript?

javascript

提问by Alan

In C# you can do the following:

在 C# 中,您可以执行以下操作:

string y = null;
string x = y ?? "sup stallion"; //x = "sup stallion" since y is null.

Where The ??operator is the null-coalescing operator.

其中??运算符是空合并运算符。

And in Javascript I've seen something similar:

在 Javascript 中,我看到了类似的东西:

var headers;
var myHeader = headers || {'Content-type':'text/plain'}; //myHeaders = {'Content...

I've also seen: (The 2nd code snippet on the page)

我还看到:(页面上的第二个代码片段)

var headers;
var myHeader = headers | {'Content-type':'text/plain'};

Is there a difference between the two? What is this pattern called...default parameter?

两者有区别吗?这个模式叫什么...默认参数?

回答by Ben Lee

||is a logical or. It returns the first truthy operand* (the last value evaluated). So

||是一个逻辑或。它返回第一个真实操作数*(评估的最后一个值)。所以

var myHeader = headers || {'Content-type':'text/plain'};

Returns "headers" if it's truthy (and if it's null or undefined, that value is coreced into "false"). If it's falsey, it returns the second operand. I don't believe this has a very specific name in javascript, just something general like "default argument value".

如果它是真的,则返回“headers”(如果它为 null 或未定义,则该值被核心化为“false”)。如果为假,则返回第二个操作数。我不相信这在 javascript 中有一个非常具体的名称,只是像“默认参数值”这样的通用名称。

|is a bitwise or. It is a mathematical operation and does something completely different. That operator doesn't even make sense here (and it will generally just produce 0as a result). Wherever you saw that, it was surely a typo, and they meant to use logical or.

|按位或。这是一个数学运算,并且做一些完全不同的事情。该运算符在这里甚至没有意义(并且通常只会产生0结果)。无论你在哪里看到,这肯定是一个错字,他们的意思是使用逻辑或。

So go with that first method (a = b || c).

因此,请使用第一种方法 ( a = b || c)。

* "Logical or" is also known as "logical disjunction" or "inclusive disjunction". Javascript, like all programming languages, evaluates logical statements using short-circuit evaluation. With logical-or expressions, it evaluates each operand for truthiness and stops on the first true one (and returns that value). If there are no truthy operands, it still had to go through all of them, so it returns the last operand, still the last one it evaluated. Logical and (&&) is similarly short-circuited by stopping on the first falsey operand.

* “逻辑或”也称为“逻辑析取”或“包含析取”。与所有编程语言一样,Javascript 使用短路评估来评估逻辑语句。使用逻辑或表达式,它评估每个操作数的真实性并在第一个真值处停止(并返回该值)。如果没有真实操作数,它仍然必须遍历所有操作数,因此它返回最后一个操作数,仍然是它评估的最后一个。逻辑和 (&&) 类似地通过在第一个错误操作数上停止而短路。

回答by austincheney

I am not familiar with the second pattern. The two patterns I am aware of:

我对第二种模式不熟悉。我知道的两种模式:

1) Your first pattern is a basic logical or operator. If the first value is falsy then the second value is assigned.

1)你的第一个模式是一个基本的逻辑或运算符。如果第一个值是假的,则分配第二个值。

2) The second pattern is called a ternary assignment, which is similar in logic to a basic if condition, but the syntax is slightly different.

2)第二种模式称为三元赋值,在逻辑上与基本的if条件类似,但语法略有不同。

var test = (typeof myTest === "string") ? firstValue : secondValue;

In this pattern the question mark separates the condition from the values and the colon separates the values. Tertiary assignments can be nested so that one of the values contains another tertiary.

在这种模式中,问号将条件与值分开,冒号将值分开。三级分配可以嵌套,以便其中一个值包含另一个三级分配。

回答by Joseph

Not really an expert to this but the ||is a Logical Operatorand |is a Bitwise Operator

不是真正的专家,但它||逻辑运算符并且|位运算符