From fd25d575600764f45934e591c3c78a274959a36c Mon Sep 17 00:00:00 2001 From: allo Date: Sat, 4 Dec 2021 15:05:54 +0800 Subject: convert to markdown and translate to zh-CN --- .../operators/logical_or_assignment/index.md | 105 ++++++++++----------- 1 file changed, 49 insertions(+), 56 deletions(-) (limited to 'files/zh-cn/web/javascript/reference') diff --git a/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.md b/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.md index ddd0dc8c79..6c4f2c2e1e 100644 --- a/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.md +++ b/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.md @@ -1,87 +1,80 @@ --- title: Logical OR assignment (||=) slug: Web/JavaScript/Reference/Operators/Logical_OR_assignment +tags: + - JavaScript + - Language feature + - Logical Operator + - Operator + - Reference translation_of: Web/JavaScript/Reference/Operators/Logical_OR_assignment --- -
{{jsSidebar("Operators")}}
+{{jsSidebar("Operators")}} -

The logical OR assignment (x ||= y) operator only assigns if x is {{Glossary("falsy")}}.

+逻辑或赋值(`x ||= y`)运算仅在 `x` 为{{Glossary("falsy", "虚")}}值时赋值。 -
{{EmbedInteractiveExample("pages/js/expressions-logical-or-assignment.html")}}
+{{EmbedInteractiveExample("pages/js/expressions-logical-or-assignment.html")}} - +## 语法 -

 语法

+```js +expr1 ||= expr2 +``` -
expr1 ||= expr2
-
+## 描述 -

描述

+### 短路运算 -

Short-circuit evaluation

+[逻辑或](/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_OR)的运算方法如下所示: -

The logical OR operator works like this:

+```js +x || y; +// 当 x 为真值时,返回 x +// 当 y 为真值时,返回 y +``` -
x || y;
-// returns x when x is truthy
-// returns y when x is not truthy
+逻辑或运算的短路逻辑:当且仅当第一个操作数尚未确定结果(不是真值)时,才会评估第二个操作数。 -

The logical OR operator short-circuits: the second operand is only evaluated if the first operand doesn’t already determine the result.

+逻辑或赋值运算有短路逻辑,这意味着,它仅在左侧为虚值时执行赋值。换句话说,`x ||= y` 等同于: -

Logical OR assignment short-circuits as well, meaning it only performs an assignment if the logical operation would evaluate the right-hand side. In other words, x ||= y is equivalent to:

+```js +x || (x = y); +``` -
x || (x = y);
-
+但不等同于以下总是执行赋值的语句: -

And not equivalent to the following which would always perform an assignment:

+```js example-bad +x = x || y; +``` -
x = x || y;
-
+请注意,这与数学逻辑和按位赋值运算不同。 -

Note that this behavior is different to mathematical and bitwise assignment operators.

+## 示例 -

例子

+### 设定默认值 -

Setting default content

+当“lyrics”元素为空时,则显示默认值: -

If the "lyrics" element is empty, set the innerHTML to a default value:

+```js +document.getElementById('lyrics').textContent ||= 'No lyrics.' +``` -
document.getElementById('lyrics').innerHTML ||= '<i>No lyrics.</i>'
+在这里,短路运算特别有用,因为元素不会产生不必要的更新,也不会引起诸如额外的解析、渲染、失去焦点等副作用。 -

Here the short-circuit is especially beneficial, since the element will not be updated unnecessarily and won't cause unwanted side-effects such as additional parsing or rendering work, or loss of focus, etc.

+注意:请注意检查 API 返回的值。如果返回的是空字符串(是{{Glossary("falsy", "虚")}}值),则必须使用 `||=`。在其他情况下(返回值是 {{jsxref("null")}} 或 {{jsxref("undefined")}}),你可以使用 `??=` 运算符。 -

Note: Pay attention to the value returned by the API you're checking against. If an empty string is returned (a {{Glossary("falsy")}} value), ||= must be used, otherwise you want to use the ??= operator (for {{jsxref("null")}} or {{jsxref("undefined")}} return values).

+## 规范 -

规范

+{{Specifications}} - - - - - - - - - - - - - -
Specification
{{SpecName('Logical Assignment', '#sec-assignment-operators', 'Assignment operators')}}
+## 浏览器兼容性 -

浏览器兼容性

+{{Compat}} +## 参见 - -

{{Compat("javascript.operators.logical_or_assignment")}}

- -

参见

- - +- [逻辑或(||)](/zh-CN/docs/Web/JavaScript/Reference/Operators/Logical_OR) +- [空值合并运算符(`??`)](/zh-CN/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator) +- [按位或赋值(`|=`)](/zh-CN/docs/Web/JavaScript/Reference/Operators/Bitwise_OR_assignment) +- {{Glossary("Truthy")}} +- {{Glossary("Falsy")}} -- cgit v1.2.3-54-g00ecf