Understanding Incrementing

前端 未结 7 2125
别那么骄傲
别那么骄傲 2020-11-27 22:31

For example this:

var a = 123;
var b = a++;

now a contains 124 and b contains 123

7条回答
  •  抹茶落季
    2020-11-27 23:13

    Note that you can also write

    b = ++a;
    

    Which has the effect you are probably expecting.

    It's important to realise that there are two things going on here: the assignment and the increment and the language should define in which order they will happen. As we have available both ++a and a++ it makes sense that they should have different meanings.

    For those of us from a C background, this is quite natural. If PHP behaves differently, we might be wondering why PHP chose to deviate from what we are accustomed to.

提交回复
热议问题