在JavaScript中简单地替换逻辑的赋值运算符? [英] Simple substitute of assignment operators of logical ones in JavaScript?
问题描述
JavaScript已赋值运算符对应算术: + =
, - =
, * =
, / =
,%=
。
JavaScript has assignment operators corresponding to arithmetic ones: +=
, -=
, *=
, /=
, %=
.
JavaScript也有赋值运算符对应按位:<< =
, >> =
,>>> =
,& =
, ^ =
, | =
。
JavaScript also has assignment operators corresponding to bitwise ones: <<=
, >>=
, >>>=
, &=
, ^=
, |=
.
然后,我不能做这样的事情
Then, I can't do things like
aVeryLongVariableIdontWantToRepeat ||= 1;
在另一个问题它解释了为什么 JS Java没有这样的运算符。我想这对JS来说是一样的。
In this other question it's explained why JS Java doesn't have such operators. I guess it's the same for JS.
但我想知道是否有一种简单的方法来模仿它们,避免
But I want to know if there is a simple way to emulate them, avoiding
aVeryLongVariableIdontWantToRepeat = aVeryLongVariableIdontWantToRepeat || 1;
推荐答案
不,没有。我觉得应该有更多这个答案,但实际上,就是这样。最短版本的 a = a || x
是... a = a || x
。
No, there isn't. I feel like there should be more to this answer, but really, that's it. The shortest version of a = a || x
is ... a = a || x
.
这篇关于在JavaScript中简单地替换逻辑的赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!