javascript运算符
The three new logical operators: nullish, AND, and OR are now available
现在提供了三个新的逻辑运算符:空值,AND和OR
Want to write less code?
想写更少的代码吗?
Let’s meet the new assignment operators available in JavaScript, which are readily available and in Firefox 79, and Chrome 85 (not available in Node.js quite yet).
我们来看看JavaScript中提供的新赋值运算符,这些赋值运算符在Firefox 79和Chrome 85中很容易获得,而在Chrome 85 (Node.js中尚不可用)。
The logical assignment operator proposal specifies new logical operators to help quickly write cleaner assignment JavaScript code.
逻辑分配运算符建议指定新的逻辑运算符,以帮助快速编写更简洁的分配JavaScript代码。
There’s the QQ equals (logical nullish assignment), the And And Equals (logical AND assignment), and the Or Or Equals (logical OR assignment), each offering a better way to update and assign values using shorthand convenience operators.
有QQ等于(逻辑为空的分配),与与等于(逻辑与的分配)和或与等于(逻辑或的分配),每一个都提供了使用速记便捷运算符更新和分配值的更好方法。
The new operators have the same short circuit behavior of the existing common logical operations that are already implemented, such as plus equals (+=
a.k.a addition assignment) and all of the other useful compound assignment operators that JavaScript has to offer.
新运算符具有与已经实现的现有常见逻辑运算相同的短路行为 ,例如JavaScript必须提供的加等于( +=
aka加法运算)和所有其他有用的复合运算符。
I think these new operators are extremely useful because they allow for a handy way to change assignments only when certain logical conditions are met.
我认为这些新运算符非常有用,因为它们仅在满足某些逻辑条件时才允许使用便捷的方式来更改分配。
我们不会产生有害的副作用,只是通过快速,简洁的操作将值简单地合并为变量即可。 (We don’t get unwanted side effects, just simple coalescing of values into variables with quick, concise operations.)
Let’s look at the three new operators.
让我们看一下这三个新的运算符。
逻辑无效分配(?? =) (Logical nullish assignment (??=))
The first one is the logical nullish assignment. The logical nullish assignment only assigns if the left-hand variable is nullish, which in JavaScript means it is either null
or undefined
. See also the nullish coalesce operator.
第一个是逻辑上的无效分配 。 逻辑空值分配仅在左侧变量为null
时才分配,这在JavaScript中意味着它为null
或undefined
。 另请参见无效的合并运算符 。
By writing this statement:
通过编写以下语句:
x ??= y
The equivalent logic translates into something like this:
等效逻辑转化为如下形式:
x ?? (x = y);
例子 (Examples)
const book = { title: 'Dogs' };
// book.title is NOT nullish
book.title ??= 'Cats';
console.log(book.title);
// expected output: 'Dogs'
// book.author is nullish
book.author ??= 'Cameron Manavian';
console.log(book.author);
// expected output: 'Cameron Manavian'
So if x
is nullish, this operator will update the value, but if x
is NOT nullish, it will leave the existing value intact.
因此,如果x
为空,则此运算符将更新该值,但如果x
不为空,则将保留现有值不变。
逻辑AND赋值(&& =) (Logical AND assignment (&&=))
The next operator is the logical and assignment. The logical AND assignment only assigns if the left-hand variable is truthy, which in JavaScript means it is anything that is not falsy — anything that isn’t false
, 0
, ""
, null
, undefined
, and NaN
and a few more values.
下一个运算符是逻辑和赋值 。 如果左边的变量是逻辑与分配唯一受让人truthy ,这在JavaScript意味着它是什么,是不是falsy任何不是- false
, 0
, ""
, null
, undefined
,并NaN
和一些更值。
This one works just like how you’d use the Logical AND where the right side of the &&
doesn't get evaluated unless the expression on the left side is truthy.
就像您使用逻辑AND的方式一样 ,除非在左侧的表达式是真实的,否则不会对&&
的右侧进行求值。
By writing this statement:
通过编写以下语句:
x &&= y
The equivalent logic translates into something like this:
等效逻辑转化为如下形式:
x && (x = y);
例子 (Examples)
const book = { title: 'Dogs', description: '' };
// book.title is truthy
book.title &&= 'Cats and Dogs';
console.log(book.title);
// expected output: 'Cats and Dogs'
// book.author is falsy
book.author &&= 'Cameron Manavian';
console.log(book.author);
// expected output: undefined
// book.description is falsy
book.description &&= 'This is a short book!';
console.log(book.description);
// expected output: ''
So if x
is truthy, this operator will update the value, but if x
is falsy, it will leave the existing value intact.
因此,如果x
为真,则此运算符将更新该值,但如果x
为假,则将保留现有值不变。
逻辑或分配(|| =) (Logical OR assignment (||=))
Finally, we have the logical or assignment. The logical OR assignment only assigns if the left-hand variable is falsy — again, anything that equals false
, 0
, ""
, null
, undefined
, and NaN
and a few more values.
最后,我们有逻辑或赋值 。 逻辑OR赋值仅在左侧变量为falsy时才赋值 -再次赋值为false
, 0
, ""
, null
, undefined
和NaN
以及其他一些值。
This one is the exact opposite of the previous operator, and as such, is also just like the Logical OR used heavily in JavaScript logic within boolean expressions.
这与上一个运算符完全相反 ,因此,就像布尔表达式中JavaScript逻辑中大量使用的Logical OR 。
By writing this statement:
通过编写以下语句:
x ||= y
The equivalent logic translates into something like this:
等效逻辑转换为如下形式:
x || (x = y);
例子 (Examples)
const book = { title: 'Birds', author: '' };
// book.title is truthy
book.title ||= 'Cats and Dogs';
console.log(book.title);
// expected output: 'Birds'
// book.author is falsy
book.author ||= 'Cameron Manavian';
console.log(book.author);
// expected output: 'Cameron Manavian'
Enjoy!
请享用!
javascript运算符