2

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:

  • Download the Development branch of Flanvas
  • Make sure you place the folder in web-server directory (otherwise you will encounter cross-domain issues)
  • Run the page examples/loader-svg.js
  • Look at the console to see the mix-up I'm referring to.

*Notes

  • The console log in question (Exhibit A) located in src/flanvas.js on line 2958
  • The framework has recently undergone a "shload" of updates and you'll notice not everything works -- I am aware.
  • I'm testing on Mac 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?

7
  • @harto 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. In this case it isn't. I'll add this to an edit as well. Commented Dec 6, 2011 at 1:13
  • 1
    Isn't this a question for the framework developer? Commented Dec 6, 2011 at 1:21
  • @Jackson - are you saying Exhibit A on its own is enough to demonstrate the problem? Or is it only a problem in the framework code (which presumably does other stuff with the array before and after the console.log)? Commented Dec 6, 2011 at 1:37
  • @Jackson, pt[0] will become 0, pt will become a reference to the array. If console.log waits for a while before turning the array into a string, 0 will still be 0, but the array may have changed. Commented Dec 6, 2011 at 1:50
  • 1
    @nnnnnn Exhibit A by it's self is just fine. Relative to it's block it is not fine, hence my confusion. @@James Clark That is probable, but seems highly unlikely. @@@RightSaidFred You sir, are on your game. I overwrite the same variable because I assume that will "reset" the variable and I'm in no danger of running into "old content". However, it seems like "old content" is in fact what is being displayed because JS is honoring rules that are either bizarre or new-to-me (likely the latter). Commented Dec 6, 2011 at 4:47

1 Answer 1

4

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.


demonstration

Here's a demo for illustrative purposes.

http://jsfiddle.net/J7tbB/

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.

http://jsfiddle.net/J7tbB/1/

console.log( arr.slice() );

// ...

[0, 0, 0, 0, 0]
Sign up to request clarification or add additional context in comments.

2 Comments

That seems to be a quirk of Google Chrome. I get all zeros in FireFox and IE.
@gilly3: That could be. I normally test in Chrome, and I've noticed this out of sync behavior in the past when passing objects to console.log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.