在 JavaScript 中增加变量的其他方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6594325/
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 22:24:39  来源:igfitidea点击:

Other ways to increment a variable in JavaScript

javascript

提问by Daniel Attfield

Possible Duplicate:
Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

可能的重复:
为什么在 JavaScript 中避免使用递增(“++”)和递减(“--”)运算符?

I'm a big fan of Douglas Crockford and his excellent book, JavaScript: The Good Parts. I also use his JSLinttool on an hourly basis before checking in any code to our repository, as is good sense.

我是 Douglas Crockford 和他的优秀著作JavaScript: The Good Parts 的忠实粉丝。在将任何代码签入我们的存储库之前,我还每小时使用他的JSLint工具,这很有意义。

One thing I've noticed when running code through JSLint is its insistence that the ++ increment operator is somehow evil. I know I can turn certain rules off, but that's cheating ;). Crockford mentions his dislike on page 112 of JS: TGP...

在通过 JSLint 运行代码时,我注意到的一件事是它坚持认为 ++ 增量运算符在某种程度上是邪恶的。我知道我可以关闭某些规则,但那是作弊;)。Crockford 在 JS: TGP 的第 112 页提到了他的不喜欢...

In my own practice, I observed that when I used ++ and --, my code tended to be too tight, too tricky, too cryptic. So, as a matter of discipline, I don't use them any more. I think that as a result, my coding style has become cleaner.

在我自己的实践中,我观察到当我使用 ++ 和 -- 时,我的代码往往过于紧凑、过于棘手、过于神秘。所以,作为纪律问题,我不再使用它们。我认为因此,我的编码风格变得更清晰了。

That's all very lovely, but he doesn't give any examples of the way he codes them now. I assume he's doing something like...

这一切都非常可爱,但他没有给出任何关于他现在编码方式的例子。我猜他正在做类似的事情...

var i;
i = 0;
i = i + 1;

Again, great, but I've got a few basic 'for loops' in my JS code, as I imagine many people have, and I've always used the standard syntax...

再次,很好,但是我的 JS 代码中有一些基本的“for 循环”,我想很多人都有,而且我一直使用标准语法......

for (i = 0; i < myArray.length; i++) { 
    // Loop Stuff 
}

Am I missing something? What's the cleanest and/or best way to increment/decrement?

我错过了什么吗?增加/减少的最干净和/或最好的方法是什么?

回答by b_erb

I think this is rather controversial, and I personally stick to i++in loops. Of cource, you can replace it with i = i + 1in your loop statement, to be fully compliant to JSLint.

我认为这是相当有争议的,我个人坚持i++循环。当然,您可以i = i + 1在循环语句中替换它,以完全符合 JSLint。

There aren't real alternatives to increment and decrement numerical values in JavaScript. You can often use Array.forEach()and/or Object.keys()in order to prevent numerical indexes.

在 JavaScript 中增加和减少数值没有真正的替代方案。您可以经常使用Array.forEach()和/或Object.keys()来防止数字索引。

回答by ascanio

The only tricky thing I see in autoincrement/decrement operators (and I'm quite surprised that no one here pointed that out) is the difference between prefix (++i) and postfix (i++) versions. I also believe that autoincrement/decrement operators are slightly more efficient, in general. Besides this, I think the choice is based mainly on aesthetics, especially in javascript.

我在自增/自减运算符中看到的唯一棘手的事情(我很惊讶这里没有人指出这一点)是前缀 (++i) 和后缀 (i++) 版本之间的区别。我也相信自动递增/递减运算符的效率一般稍高一些。除此之外,我认为选择主要基于美学,尤其是在 javascript 中。

回答by Alvin

to pass JSLint I now always do:

通过 JSLint 我现在总是这样做:

for (var i = 0; i < myArray.length; i+=1) { 
    // Loop Stuff 
}

回答by Kevin Bowersox

Personally, I see nothing wrong with the ++/-- operators for incrementing and decrementing. While something like i = i + 1;may be easier for new coders, the practice is not rocket science once you know what the operators stand for.

就个人而言,我认为 ++/-- 运算符用于递增和递减没有任何问题。虽然i = i + 1;对于新的编码员来说类似的事情可能更容易,但一旦你知道操作员代表什么,这种做法就不是火箭科学。

回答by Shadow Wizard is Ear For You

Regarding loop, one original idea can be using such loop to avoid all kinds of numbers:

关于循环,一个原始想法可以使用这样的循环来避免各种数字:

var a = [];
for (var i in "     ")
    a.push("Loop iteration here");
alert(a.join("\n"));

Meaning iterate over a string, there will be amount of iterations equal to the string length - 5 in the above example.

意思是迭代一个字符串,在上面的例子中,迭代次数等于字符串长度 - 5。

Live test case: http://jsfiddle.net/vTFDP/

现场测试用例:http: //jsfiddle.net/vTFDP/

回答by spraff

In C/C++ this is important, especially with arbitrary iterators and pre/post increment semantics. Also, random access vs forward-iterators.

在 C/C++ 中,这很重要,尤其是对于任意迭代器和前/后增量语义。此外,随机访问与前向迭代器。

In Javascript it's more about aesthetics than anything else. Unless you're dealing with strings. "1"+1is "11".

在 Javascript 中,它更多地是关于美学的。除非你正在处理字符串。"1"+1是“11”。

I would say that ++i is an idiom that's worth being consistent with.

我会说 ++i 是一个值得保持一致的习语。

JSLint is wrong.

JSLint 是错误的。