Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

前端 未结 16 1000
庸人自扰
庸人自扰 2020-11-22 06:23

One of the tips for jslint tool is:

++ and --
The ++ (increment) and -- (decrement) operators have been known to contribute

16条回答
  •  旧巷少年郎
    2020-11-22 06:34

    The most important rationale for avoiding ++ or -- is that the operators return values and cause side effects at the same time, making it harder to reason about the code.

    For efficiency's sake, I prefer:

    • ++i when not using the return value (no temporary)
    • i++ when using the return value (no pipeline stall)

    I am a fan of Mr. Crockford, but in this case I have to disagree. ++i is 25% less text to parse than i+=1 and arguably clearer.

提交回复
热议问题