简体   繁体   English

这个语法到底在做什么?

[英]What is this syntax exactly doing?

What is exactly happening in the following line? 下一行到底发生了什么?

return [selector, operation || "=", true];

I understand what returning is, the OR operator, etc. I've never seen this exact syntax. 我知道返回的是什么,或运算符,等等。我从未见过这种确切的语法。

It means: 它的意思是:

Return an array of 3 elements: selector , operation (unless it's falsy , then "=" ), and true . 返回一个由3个元素组成的数组: selectoroperation (除非是假的 ,然后是"=" )和true

The line is parsed with the || 该行用||解析。 operator binding tighter than the separating commas. 操作员绑定比分隔逗号更紧密。 So the line is equivalent to return [selector, (operation || "="), true]; 因此,该行等效于return [selector, (operation || "="), true];

In javascript, the || 在javascript中, || operator doesn't just work with boolean values. 运算符不仅适用于布尔值。 The actual semantics for return (a || b) is: "if (a is truthy ) return a; else return b;". return (a || b)的实际语义是:“如果(a是true )返回a;否则返回b;”。

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR 请参阅: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Logical_OR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM