那么是的最佳实践是什么?
Jslint 解释说它“增加了困惑”。真的没看出来……
编辑:代码,根据要求:
var all,l,elements,e;
all = inElement.getElementsByTagName('*');
l = all.length;
elements = [];
for (e = 0; e < l; (e++))
{
if (findIn)
{
if (all[e].className.indexOf(className) > 0)
{
elements[elements.length] = all[e];
}
} else {
if (all[e].className === className)
{
elements[elements.length] = all[e];
}
}
}
最佳答案
长期的最佳实践:按照 jslint 的建议改用 i += 1
。
至于为什么它比 ++
更好,根据 Crockford 的说法:
The increment
++
and decrement--
operators make it possible to write in an extremely terse style. In languages such as C, they made it possible to write one-liners that:for (p = src, q = dest; !*p; p++, q++) *q = *p;
Most of the buffer overrun bugs that created terrible security vulnerabilities were due to code like this. 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.
编辑:包含来自 Nope 的评论随着这个答案不断得到关注。
关于javascript - jslint中的 "unexpected++"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3000276/