gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:03:55 26 4
gpt4 key购买 nike

tips for jslint tool 之一是:

++ and --
The ++ (increment) and -- (decrement)operators have been known to contribute to bad code byencouraging excessive trickiness. Theyare second only to faulty architecturein enabling to viruses and othersecurity menaces. There is a plusplusoption that prohibits the use of theseoperators.

我知道像 $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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com