Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't console.log() take a snapshot of the passed variables?

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 happening or if there is some other magic involved. So this is my code:

    var SomeObject = {};

    SomeObject.foo = function(a, b) {
       var baz = this.bar(a, b);
       console.log(baz);
       console.log(baz.left);
       SomeObject.magicalStuff(baz);
    };

    SomeObject.bar = function(a, b) {
        return {left: a-b, top: b-a};
    };

    SomeObject.magicalStuff = function(position) {
        position.left = 0;
    };

    SomeObject.foo(100, 50);

The code at jsFiddle

The output of this is something like (depending on the browser):

> Object
50

If you expand the "Object" (in Chrome, Safari or Firefox (Firebug) what you get is:

> Object
    left: 0
    top: -50

Whereas I would expect:

> Object
    left: 50
    top: -50

What I think is going on is that console.log() really just "posts" a reference to the console, which gets read once you click on the "expand" symbol. But doesn't that kind of defeat the purpose of console.log() as a debugging instrument? I always expected console.log() to "snapshot" the stuff I pass to it. It is really surprising to see a statement which comes after the actual console.log() change the output of that very console.log() call.

Or is there something else going on?

Edit: I'm also wondering if there is a sound reason for browser developers to implement console.log like this (I guess there is one, otherwise it wouldn't be consistent across major browsers).

like image 263
fresskoma Avatar asked Mar 07 '11 18:03

fresskoma


People also ask

Why does console log work but not return?

console. log will log to the console (as the name suggests) while return just ends the function and passes the value to whatever called that function. Since you're not using that return value, you won't see anything but it is produced.

What does console log () do?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user. Syntax: console.

Does console log return anything?

The console. log() is a function that writes a message to log on the debugging console, such as Webkit or Firebug. In a browser you will not see anything on the screen. It logs a message to a debugging console.

How do you assign a console log to a variable?

If you want to do this to an object that has been already logged (one time thing), chrome console offers a good solution. Hover over the printed object in the console, right click, then click on "Store as Global Variable". Chrome will assign it to a temporary var name for you which you can use in the console.


2 Answers

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)...

like image 162
user123444555621 Avatar answered Sep 20 '22 06:09

user123444555621


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().

like image 24
Nathan Ostgard Avatar answered Sep 22 '22 06:09

Nathan Ostgard