Javascript +=(加等号)如何工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6826260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:20:43  来源:igfitidea点击:

How does += (plus equal) work?

javascript

提问by muudless

I'm a bit confused with the += sign. How does it work?

我对 += 符号有点困惑。它是如何工作的?

  1. 1 += 2// equals ?

  2. and this

    var data = [1,2,3,4,5];
    var sum = 0;
    data.forEach(function(value) {
        sum += value; 
    });
    sum = ?
    
  1. 1 += 2// 等于 ?

  2. 和这个

    var data = [1,2,3,4,5];
    var sum = 0;
    data.forEach(function(value) {
        sum += value; 
    });
    sum = ?
    

回答by lawnsea

1 += 2is a syntax error (left-side must be a variable).

1 += 2是语法错误(左侧必须是变量)。

x += yis shorthand for x = x + y.

x += y是 的简写x = x + y

回答by Paul

1) 1 += 2 // equals ?

1) 1 += 2 // 等于 ?

That is syntactically invalid. The left side must be a variable. For example.

这在语法上是无效的。左边必须是一个变量。例如。

var mynum = 1;
mynum += 2;
// now mynum is 3.

mynum += 2;is just a short form for mynum = mynum + 2;

mynum += 2;只是一个简短的形式 mynum = mynum + 2;

2)

2)

var data = [1,2,3,4,5];
var sum = 0;
data.forEach(function(value) {
    sum += value; 
});

Sum is now 15. Unrolling the forEach we have:

总和现在是 15。展开我们拥有的 forEach:

var sum = 0;
sum += 1; // sum is 1
sum += 2; // sum is 3
sum += 3; // sum is 6
sum += 4; // sum is 10
sum += 5; // sum is 15

回答by Tudor Constantin

That is just a short form for:

这只是一个简短的形式:

sum = sum + value;

回答by Paul

+=in JavaScript (as well as in many other languages) adds the right hand side to the variable on the left hand side, storing the result in that variable. Your example of 1 +=2therefore does not make sense. Here is an example:

+=在 JavaScript(以及许多其他语言)中,将右侧添加到左侧的变量中,将结果存储在该变量中。1 +=2因此,您的示例没有意义。下面是一个例子:

var x = 5;
x += 4; // x now equals 9, same as writing x = x + 4;
x -= 3; // x now equals 6, same as writing x = x - 3;
x *= 2; // x now equals 12, same as writing x = x * 2;
x /= 3; // x now equals 4, same as writing x = x / 3;

In your specific example the loop is summing the numbers in the array data.

在您的具体示例中,循环正在对数组中的数字求和data

回答by cillierscharl

+=operator is used to concatenate strings or add numbers.

+=运算符用于连接字符串或添加数字。

It will increment your sum variable with the amount next to it.

它将使用旁边的金额增加您的 sum 变量。

var sum = 0;
var valueAdded = 5; 

sum += valueAdded;

sum = 5

总和 = 5

回答by Paolo

You have to know that:

你必须知道:

  • Assignment operators syntax is: variable = expression;

    For this reason 1 += 2-> 1 = 1 + 2is not a valid syntax as the left operand isn't a variable. The error in this case is ReferenceError: invalid assignment left-hand side.

  • x += yis the short form for x = x + y, where xis the variable and x + ythe expression.

    The result of the sum is 15.

  • 赋值运算符语法是: variable = expression;

    由于这个原因1 += 2->1 = 1 + 2不是有效的语法,因为左操作数不是变量。在这种情况下的错误是ReferenceError: invalid assignment left-hand side

  • x += y是 的缩写x = x + y,其中x是变量和x + y表达式。

    总和的结果是15

      sum = 0;
      sum = sum + 1; // 1
      sum = sum + 2; // 3
      sum = sum + 3; // 6
      sum = sum + 4; // 10
      sum = sum + 5; // 15

Other assignment operatorshortcuts works the same way (relatively to the standard operations they refer to). .

其他赋值运算符快捷方式的工作方式相同(相对于它们所指的标准操作)。.

回答by Sam Dutton

...and don't forget what happens when you mix types:

...不要忘记混合类型时会发生什么:

x = 127;
x += " hours "
// x is now a string: "127 hours "
x += 1 === 0;
// x is still a string: "127 hours false"

回答by ManiShankar

that's just a shorthand notation in most languages.which means that

这只是大多数语言的速记符号。这意味着

x=x+1;

x=x+1;

we can do the same operation for x-=1,x*=1,x/=1; which means

我们可以对x-=1,x*=1,x/=1做同样的操作;意思是

> x=x-1; x=x*1; x=x/1;

> x=x-1; x=x*1; x=x/1;

回答by OluO

As everyone said above

就像上面大家说的

var str = "foo"
str += " bar"
console.log(str) //will now give you "foo bar"

Check this out as well https://www.sitepoint.com/shorthand-javascript-techniques/

也检查一下https://www.sitepoint.com/shorthand-javascript-techniques/

回答by Timothy Jones

a += bis shorthand for a = a +bwhich means:

a += b是简写a = a +b,意思是:

1) 1 += 2// won't compile

1) 1 += 2// 不会编译

2) 15

2) 15