Why code:
var data=[1,2,3]; var i=0; alert(data[i++]*=2);
Differs from:
var data2=[1,2,3]; var b=0; alert(data2[b++]=data2[b++]*2);
Why code:
var data=[1,2,3]; var i=0; alert(data[i++]*=2);
Differs from:
var data2=[1,2,3]; var b=0; alert(data2[b++]=data2[b++]*2);
In the first case, the following occurs at index 0, the element is taken, multiplied by two and written there. Therefore, the result will be [2,2,3]
and output 2. After the operation i = 1
.
In the second case, the element will be taken at index 0, multiplied by 2, then b will become equal to 1. And only after this will assignment to the element with index 1. We get [2,2,3]
. and b becomes 2 (one more increment will occur).
In general, such constructions are very discouraged ( a[i++] = a[i++] * 2
), since the result is sometimes very unpredictable. In the case of C ++, the result may be completely different depending on the compiler's mood.
i
and / or b
could already have changed. And I could just remember the result. @VladD thanks, KO! - KoVadimSource: https://ru.stackoverflow.com/questions/207055/
All Articles