javascript - Wrong value in console.log -
possible duplicate:
is chrome's javascript console lazy evaluating arrays?
i have following snippets in javascript output makes me feel going wrong.
1.
a=2; console.log(a); a+=2; console.log(a);
output:2 4
; expected
2.
t=[0,2]; console.log(t); t[0]+=2; console.log(t);
output: [2,2] [2,2]
shouldn't output be [0,2] [2,2]
? and whats difference between above 2 cases results in different answers in both cases?
it's because log delayed until chrome has time (i.e. scripts releases cpu).
try understand happens :
var t=[0,2]; console.log(t); settimeout(function() { t[0]+=2; console.log(t); }, 1000);
it outputs expect.
is bug of chrome ? maybe side effect of optimization. @ least it's dangerous design...
why there difference ? suppose chrome stores temporarily must log, primary (immutable) value in first case, pointer array in last case.
Comments
Post a Comment