Is console.dir () in javascript or firefox asynchronous?

A very strange problem.

In my debugging, I found that console.dir (anArray) did not display the current value in the firebug console of the browser.

For example,

console.dir(anArray) //line 1 console.log(anArray[0].prop1) //line 2 code to change the value of anArray[0].prop1 //line 3 

anArray is an array of javascript / json object,

in the Firbug console, line 1 displays the new value that is specified in line 3,

and line 2 is the old value, and this is what I want.

The only explanation is that console.dir () is asynchronous, right ?!

my env: Windows7, Firefox 6.0.2, firebug 1.9.1, javascript lib - DOJO (but I think it has nothing to do with it.)

Thanks.

+7
source share
4 answers

The great thing with console.dir (at least in Chrome, judging by my experience, and this ) is that the extension evaluates and displays the current value of the object at the moment you are executing the extension, not the ones that are at the moment calling console.dir() .

See for example

 <html><body> open/refresh this with the javascript console open <script> var ar = new Float32Array(1); ar[0]=2; console.log(ar[0]); console.dir(ar); ar[0]=200; </script> 

When you expand the array in the console, you see a value of 200.

It should not be a mistake, and this is certainly a performance thing (an object can be very large), but the behavior is strange and potentially confusing.

In addition, I tested this, if after that the value changes, the console does not update it (therefore, it does not work like the "watch" window in the debugger).

+2
source

In short, yes. :)

I had a similar problem with Chrome in the past, not understanding my result in the console, but I did some tests and found that this is asynchronous behavior. In fact, my best bet is that it uses internaly for setTimeout () to delay the log. console.log seems to be affected in my case too.

0
source

The log console sometimes produces such errors. It is also less efficient in Internet Explorer (the usual place to look for errors), where it cannot print objects such as objects and XML documents.

I suggest that instead of using console.log you try to learn how to use the powerful debuggers available by browsers. When you stopped at a breakpoint, you can check all the variables, run arbitrary instructions on the console, and move up and down the stack path.

0
source

This is a bug in both Firefox and Chrome: start the console:

 console.log(a = {b: { c: 0}}); abc = 1; 

result in firebug:

 b: Object { c= 1 } 

result in Chrome:

 b: Object c: 1 
0
source

All Articles