I\'ve ran into some really weird behavior with javascript today. I think I got it somehow figured out now, but I\'d like to know if what I think is going on is really happen
I've also seen this behavior and it sure looks like a reference is posted. To get around this I used the clone() method in jQuery on the things I wanted to log.
Yes, this is what's going on. I would guess it's done to minimize the overhead of console.log()
calls. Things could get out of control if every object was deep cloned for every log call.
When I need to workaround this, I either JSON.stringify()
or shallow clone the object before passing it to console.log()
.
There is console.dir()
for what you want in Firebug.
In general, it is not possible to print every level of nested properties, since objects can contain circular references like var a = {}; var b = {a: a}; a.b = b;
Implementing a perfect clone
method is very hard - I guess it would have to basically just dump the whole memory, and logging would take awfully long. Think about console.log(window)
...