javascript运算符_简洁的代码:认识JavaScript中的新逻辑分配运算符

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 79Chrome 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中意味着它为nullundefined 。 另请参见无效的合并运算符

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任何不是- false0""nullundefined ,并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时才赋值 -再次赋值为false0""nullundefinedNaN以及其他一些值。

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!

请享用!

翻译自: https://levelup.gitconnected.com/concise-code-meet-the-new-assignment-operators-in-javascript-7d39137eb42e

javascript运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值