From 2723db936781b55233e26790302a45d1765f6445 Mon Sep 17 00:00:00 2001 From: allo Date: Sat, 4 Dec 2021 14:04:55 +0800 Subject: mv to .md for logical_or_assignment --- .../operators/logical_or_assignment/index.html | 87 ---------------------- .../operators/logical_or_assignment/index.md | 87 ++++++++++++++++++++++ 2 files changed, 87 insertions(+), 87 deletions(-) delete mode 100644 files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.html create mode 100644 files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.md (limited to 'files/zh-cn/web') diff --git a/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.html b/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.html deleted file mode 100644 index ddd0dc8c79..0000000000 --- a/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.html +++ /dev/null @@ -1,87 +0,0 @@ ---- -title: Logical OR assignment (||=) -slug: Web/JavaScript/Reference/Operators/Logical_OR_assignment -translation_of: Web/JavaScript/Reference/Operators/Logical_OR_assignment ---- -
{{jsSidebar("Operators")}}
- -

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

- -
{{EmbedInteractiveExample("pages/js/expressions-logical-or-assignment.html")}}
- - - -

 语法

- -
expr1 ||= expr2
-
- -

描述

- -

Short-circuit evaluation

- -

The logical OR operator works like this:

- -
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.

- -

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:

- -
x || (x = y);
-
- -

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

- -
x = x || y;
-
- -

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

- -

例子

- -

Setting default content

- -

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

- -
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.

- -

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).

- -

规范

- - - - - - - - - - - - - - -
Specification
{{SpecName('Logical Assignment', '#sec-assignment-operators', 'Assignment operators')}}
- -

浏览器兼容性

- - - -

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

- -

参见

- - 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 new file mode 100644 index 0000000000..ddd0dc8c79 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/operators/logical_or_assignment/index.md @@ -0,0 +1,87 @@ +--- +title: Logical OR assignment (||=) +slug: Web/JavaScript/Reference/Operators/Logical_OR_assignment +translation_of: Web/JavaScript/Reference/Operators/Logical_OR_assignment +--- +
{{jsSidebar("Operators")}}
+ +

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

+ +
{{EmbedInteractiveExample("pages/js/expressions-logical-or-assignment.html")}}
+ + + +

 语法

+ +
expr1 ||= expr2
+
+ +

描述

+ +

Short-circuit evaluation

+ +

The logical OR operator works like this:

+ +
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.

+ +

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:

+ +
x || (x = y);
+
+ +

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

+ +
x = x || y;
+
+ +

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

+ +

例子

+ +

Setting default content

+ +

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

+ +
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.

+ +

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).

+ +

规范

+ + + + + + + + + + + + + + +
Specification
{{SpecName('Logical Assignment', '#sec-assignment-operators', 'Assignment operators')}}
+ +

浏览器兼容性

+ + + +

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

+ +

参见

+ + -- cgit v1.2.3-54-g00ecf