1)1 + = 2 //等于?这在语法上是无效的。左侧必须是变量。例如。var mynum = 1;mynum += 2;// now mynum is 3.mynum += 2; 只是一个简短的形式 mynum = mynum + 2;2)var data = [1,2,3,4,5];var sum = 0;data.forEach(function(value) { sum += value; });现在的总和为15。展开forEach我们有:var sum = 0;sum += 1; // sum is 1sum += 2; // sum is 3sum += 3; // sum is 6sum += 4; // sum is 10sum += 5; // sum is 15
+=在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;在您的特定示例中,循环将对数组中的数字求和data。