简体   繁体   English

为什么空格会破坏我的JavaScript代码?

[英]why do spaces break my code in javascript?

why is el+ = v[i] different from el+=v[i] ? 为什么el+ = v[i]el+=v[i] i thought javascript doesn't care about spacing. 我以为javascript不在乎空格。 thanks, Any answers are appreciated it. 谢谢,任何答案表示赞赏。 and please don't put my question on hold. 而且请不要搁置我的问题。 i'm here trying to learn. 我在这里试图学习。

var v = ['a','b','c','d','e'];
var el="";

for(i=0; i<v.length; i++){
    el+ = v[i]; // not working because of spaces, but why?
    // el+=v[i]; // working
}
document.write(el);

+= is an operator. +=是运算符。 It is not a combination of + and = . 它不是+=的组合。

Because += is an augmented (or compound) assignment operator, not + = . 因为+=扩充(或复合)赋值运算符,所以不是+ = Similarly, i++ is fine, but i+ + causes a syntax error. 同样, i++很好,但是i+ +会导致语法错误。

el+ =

it's illegal operator. 这是非法经营者。

+=
-=
/=
*=

are available for instance 例如可用

for your case. 对于你的情况。 I will suggest even avoid for loop and do instead : 我甚至建议避免for循环,而改为:

var el = v.join('');

The JavaScript engine interprets "+ =" differently from "+=". JavaScript引擎对“ + =”和“ + =”的解释不同。 That's just the way it is written. 那就是它的编写方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM