tips for jslint
tool 之一是:
++
and--
The++
(increment) and--
(decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces. There is a plusplus option that prohibits the use of these operators.
我知道像 $foo[$bar++]
这样的 PHP 结构可能很容易导致一个错误,但我想不出比以下更好的方法来控制循环:
while( a < 10 ) do { /* foo */ a++; }
或
for (var i=0; i<10; i++) { /* foo */ }
jslint
是否突出显示它们,因为有些类似的语言缺少“++
”和“--
”语法或句柄是不同的,还是有其他理由可以避免我可能遗漏的“++
”和“--
”?
最佳答案
我的观点是始终在一行中单独使用++ 和 -- ,如下所示:
i++;
array[i] = foo;
而不是
array[++i] = foo;
除此之外的任何事情都可能会让一些程序员感到困惑,并且在我看来是不值得的。 For 循环是一个异常(exception),因为增量运算符的使用是惯用的,因此总是很清楚。
关于javascript - 为什么要避免 JavaScript 中的递增 ("++") 和递减 ("--") 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/971312/