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

前端 未结 16 1265
庸人自扰
庸人自扰 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:39

    In my experience, ++i or i++ has never caused confusion other than when first learning about how the operator works. It is essential for the most basic for loops and while loops that are taught by any highschool or college course taught in languages where you can use the operator. I personally find doing something like what is below to look and read better than something with a++ being on a separate line.

    while ( a < 10 ){
        array[a++] = val
    }

    In the end it is a style preference and not anything more, what is more important is that when you do this in your code you stay consistent so that others working on the same code can follow and not have to process the same functionality in different ways.

    Also, Crockford seems to use i-=1, which I find to be harder to read than --i or i--

提交回复
热议问题