javascript运算符_JavaScript赋值运算符

javascript运算符

Compound assignment operators can be thought of as simple shortcuts to mathematical expressions. An assignment operator assigns a value to its left operand as the result of operating on its right operand.

复合赋值运算符可以被认为是数学表达式的简单捷径。 赋值运算符为其左操作数赋值,作为对其右操作数进行运算的结果。

The simplest possible operand is the = operator:

最简单的操作数是=运算符:

var x = y;

…which assigns the value of y to x. It’s also possible to chain = operators together. If we have several variables a, b and c:

…将y的值分配给x 。 也可以将=运算符链接在一起。 如果我们有几个变量 abc

var a = 5,
b = 10,
c = 15;

We can create the expression:

我们可以创建表达式:

a = b = c;

…which will set the value of both a and b to 15. (You could read the expression as “a is equal to b which is equal to c”).

…这会将ab的值都设置为15。(您可以将表达式读为“ a等于b等于c”)。

More complex combined assignment operators include:

更复杂的组合赋值运算符包括:

NameShorthand OperatorEquivalent To
Additionx += yx = x + y
Subtractionx -= yx = x - y
Multiplicationx *= yx = x * y
Divisionx /= yx = x / y
Remainderx %= yx = x % y
名称 速记运算符 相当于
加成 x += y x = x + y
减法 x -= y x = x - y
乘法 x *= y x = x * y
x /= y x = x / y
x %= y x = x % y

Because addition is also concatenation in JavaScript, the += assignment operator is often used to merge strings:

因为加法在JavaScript中也是串联的,所以+=赋值运算符通常用于合并字符串

var welcome = "Good ";
var time = "morning";
welcome += time;
console.log(welcome);

> Good morning

Note that these operators always alter the value of the left operand (the x variable, in this case). If you wanted to preserve the original value of the variable for future reference, you’d have to assign the result to a new variable:

请注意,这些运算符始终会更改左操作数的值(在这种情况下为x变量)。 如果要保留变量的原始值以供将来参考,则必须将结果分配给新变量:

var z = x + y;

其他怪癖 (Additional Quirks)

As with the ordinary addition operator, there are a few oddities, due to the fact that addition also works to concatenate:

与普通加法运算符一样 ,由于加法也可以串联

If we have a variable named boo that has a Boolean true value:

如果我们有一个名为boo的变量,其布尔true

var boo = true;

Then adding a boolean or a number to it will result in numerical addition:

然后向其添加布尔值或数字将导致数字加法:

boo += true
> 2
boo += 2
> 4

boo now contains a numerical value; adding a string to it will result in concatenation:

boo现在包含一个数值 ; 向其添加字符串将导致串联:

boo += "scary"
> "4scary"

Adding a string to a boolean value also results in concatenation.

将字符串添加到布尔值也会导致串联。

必须使用赋值运算符吗? (Do you have to use assignment operators?)

Compound assignment operators can appear confusing at first. It’s important to remember that they’re just JavaScript shorthand: a more compact way of writing things. Feel free to use the more traditional “longhand” methods if you want to; just know that an alternative exists, and try them out once in a while to see if they fit your coding style.

复合赋值运算符起初可能会令人困惑。 重要的是要记住,它们只是JavaScript的简写:一种更紧凑的编写方式。 如果需要,可以随意使用更传统的“长手”方法; 只要知道存在替代方法,就不时尝试一下,看看它们是否适合您的编码风格。

There are also more complex assignment operators, some of them introduced in the most recent version of JavaScript, which I’ll cover in the next article.

还有更复杂的赋值运算符,其中一些是在最新版本JavaScript中引入的,我将在下一篇文章中介绍。

翻译自: https://thenewcode.com/117/JavaScript-Assignment-Operators

javascript运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值