Javascript 中的递增/递减运算符是否被视为不良形式?
我最近通过 JSLint 运行所有 javascript,它总是在我的增量运算符上给出意外的“++”
。我还注意到有一个 JSLint 选项可以容忍 ++/--。
使用 i++/i--
是否被认为是不好的形式?
Possible Duplicate:
Why avoid increment ("++") and decrement ("--") operators in JavaScript?
I've recently been running all my javascript through JSLint and it always gives me Unexpected '++'
on my increment operators. I've also noticed there is a JSLint option to tolerate ++/--.
Is it considered bad form to use i++/i--
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确的话,它在 JSLint 中的原因是因为
++i
和i++
之间存在显着差异,并且许多开发人员不一定理解具体的含义关于添加何时完成以及与周围的其他操作相关的信息。例如:
这就是为什么 Crockford 认为这是不好的做法,并且更喜欢更明确的
+=1
。但是,总的来说,我对
++
没有这种问题;我非常乐意使用它,并且我会忽略 JSLint 告诉我不要这样做(大多数时候!)。这是 JSLint 的重要之处——它不会告诉您实际的错误;它会告诉您实际的错误。它告诉你的一切都是建议。其中一些是极好的建议(例如,您永远不应该使用
with
);有些是好的建议,但表明代码很差;其中一些只是在某些时候出现问题,应该单独考虑。作为开发人员,您需要知道为什么提出建议比实际修复它们更重要。一旦您了解了指出它们的原因,您就可以自己决定代码中的给定实例是否有问题以及如何修复它。希望有帮助。
If I understand correctly, the reason it's in JSLint is because there's a significant difference between
++i
andi++
, and many developers don't necessarily appreciate the implications of being specific about when the addition is done, in relation to other operations around it.for example:
This is why Crockford considers it bad practice, and prefers the more explicit
+=1
.However, in general, I don't have this kind of issue with
++
; I'm perfectly happy to use it, and I would ignore JSLint telling me not to (most of the time!).This is the important thing about JSLint -- it doesn't tell you about actual errors; everything it tells you is a suggestion. Some of them are excellent suggestions (eg you should never ever use
with
); some are good suggestions that indicate poor code; and some of them are only a problem some of the time and should be considered individually. As a developer it is more important that you need to know why it's making its suggesting than it is to actually fix them. Once you understand the reasons for them being pointed out, you are in a position to decide for yourself whether a given instance of it in your code is a problem or not and how to fix it.Hope that helps.
不,我想这可能会引起争议,但说真的,所有人很快都会在他们的教育或书籍或其他什么中了解到这个操作员。这个建议让我很恼火。
No, I suppose this could get argumentative, but seriously everything learns about this operator very soon in their educations or books or whatever. This suggestion annoys me.
我不认为它在 Javascript 中被特别认为是不好的形式,只是大多数程序员通常很难正确使用这些运算符。
I don't think it's specifically considered bad form in Javascript, it's just hard for most programmers to use these operators correctly in general.
使用
+=/-=
被认为是更好的做法,而不是使用++/--
。x += 1;
而不是x++;
。这应该是为了提高可读性并避免由于过度使用“代码技巧”而导致的打字错误。
就我个人而言,我发现使用此方法而不是默认的 ++/-- 运算符更有用,因为数字将由 IDE 着色并且更容易识别。
Instead of using
++/--
it's considered a better practice to use+=/-=
.x += 1;
instead ofx++;
.This is supposed to increase readability and avoid typing mistakes due to excessive use of "code tricks".
Personally I find more useful to use this method instead of the default ++/-- operators, as the number will be colored by the IDE and it's easier to spot.