Reputation: 32207
Exhibit A:
var pt = [0,0,0,0,0,0];
console.log(pt[0], pt);
sometimes the log will spit out 0 [Infinity, Infinity, Infinity, Infinity, Infinity, Infinity]
, but only when you do a SIX zeroes array for pt
. If you do five zeroes then you get an array of five zeroes as expected. The big clencher is how pt[0] can occassionally be 0
and Infinity
a the same time confusion
Steps to re-create the problem:
Development
branch of Flanvasexamples/loader-svg.js
*Notes
src/flanvas.js
on line 2958Mac OSX 10.6.8
and Chrome 15.0.874.121
If there are any problems with my instructions, please post back and I'll get to them ASAP. Thanks.
** Clarification of the question **
If I run console.log(pt[0], pt);
then I expect that the first value from the array pt
and pt[0]
will be identical. That's not the case in my Exhibit A
. Why not?
Upvotes: 2
Views: 152
Reputation: 11327
console.log()
isn't necessarily synchronous, so whatever code is causing the values to be Infinity
is likely happening later.
If you want to see the Array values in their current state, you need to be sure to capture its current value somehow.
One way is to abuse JSON for this purpose.
var pt = [0,0,0,0,0,0];
console.log(pt[0], JSON.stringify( pt ));
Or since it's an Array of primitives, you could slice
it.
var pt = [0,0,0,0,0,0];
console.log(pt[0], pt.slice());
To find the actual issue, you're going to have to follow pt
along and see who's misusing it.
Here's a demo for illustrative purposes.
var later = Date.now() + 1000;
var arr = [0,0,0,0,0];
console.log( arr );
while( Date.now() < later ) {
var len = arr.length;
while( len-- ) arr[len]++;
}
Note that this freezes your browser for 1000ms
.
As you can see, it creates an Array with 5 members initialized at 0
, then immediately passes the Array to console.log
.
Right after that you have a while
loop that will run for 1000ms
, incrementing each member once for each iteration.
If the console.log
was synchronous, you'd get your zeros. Instead you'll likely get something like:
[2811349, 2811349, 2811349, 2811349, 2811349]
Now if we change it so that it logs a .slice()
of the Array, you'll see that you get your expected values.
console.log( arr.slice() );
// ...
[0, 0, 0, 0, 0]
Upvotes: 4