Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

A very weird problem.

In my debugging, I found that console.dir(anArray) didn't output the current value on browser's firebug console.

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

the anArray is an array of javascript/json object,

in Firbug's console, line 1 output the new value, which is set in line 3,

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

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

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

Thanks.

like image 258
qwert Avatar asked Mar 13 '12 10:03

qwert


People also ask

What is console Dir in JavaScript?

dir() The method console. dir() displays an interactive list of the properties of the specified JavaScript object. The output is presented as a hierarchical listing with disclosure triangles that let you see the contents of child objects.

What is the difference between console log and console dir?

The main difference between these two methods is that the console. log() method displays the “toString” representation of any object passed to it. Whereas, the console. dir() method displays an interactive list of the properties of the specified JavaScript object.

What does console Dir stand for?

It just means "Directory".

What is the procedure to view the console in Firefox?

You can open the Browser Console in one of two ways: from the menu: select “Browser Console” from the Browser Tools submenu in the Firefox Menu (or Tools menu if you display the menu bar or are on macOS). from the keyboard: press Ctrl + Shift + J (or Cmd + Shift + J on a Mac).


1 Answers

The big thing with console.dir (at least in Chrome, judging by my experience, and by this) is that the expansion evaluates and shows the current value of the object, at the moment when you do the expansion, not those at the moment of the console.dir() call.

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 the value 200.

It's not supposed to be a bug, and it's surely a performance thing (the object can be very big), but the behaviour is weird, and potentially confusing.

Furthermore, I've tested that if the value changes afterwards, the console does not update it (so it doesn't work as a 'watch' window in a debugger neither).

like image 99
leonbloy Avatar answered Oct 06 '22 20:10

leonbloy