javascript - 为什么要避免 JavaScript 中的递增 ("++") 和递减 ("--") 运算符?

标签 javascript syntax jslint postfix-operator prefix-operator

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/

相关文章:

javascript - 用 CSS 和一点 JS 来设计输入类型文件的样式,这样可以吗?

javascript - 是否可以在两个不同的 Iframe 之间复制鼠标和键盘事件?

javascript - 当提供服务的组件被破坏时,服务也会被破坏吗?

javascript - JSLint - 在 Visual Studio 2015 中构建时忽略文件夹

javascript - 我的 Apollo 服务器的订阅不起作用 : Cannot read property 'headers' of undefined

c# - "empty line with semicolon"在 C# 中意味着什么?

c - 这是什么语法? (tty_write_unlock)

javascript - 是否可以在 Javascript 中转发声明函数或方法?

wrapper - JSLint4Java 用法

python - 如何在 Emacs 中检查 Python 代码的语法而不实际执行它?