0

Possible Duplicate:
Bizarre console.log behaviour in Chrome Developer Tools

Given the following script :

    var Test = function () {
        console.log("constructor");
        this.positions = [];
        console.log(this.positions);
        console.log("constructorEnd");
        this.addPosition = function () {
            console.log(this.positions);
            var i = this.positions.length;
            this.positions[i] = "aa";
            console.log(this.positions);
        }
    }
    var t = new Test();
    console.log("constructed");
    t.addPosition();
}

The expected result is :

constructor
[]
constructorEnd
constructed
[]
["aa"]

This is what I can see using firebug. The very same script on chrome (linux, 16.0.912.21 dev) gives me :

constructor
["aa"]
constructorEnd
constructed
["aa"]
["aa"]

What does that mean ?? Adding a breakpoint in chrome to see what happen make it works (same ouput as firefox) ! Here is a testcase : http://jsfiddle.net/sNzKq/1/

thanks

Community
  • 1
  • 1
Quentin
  • 3,150
  • 4
  • 24
  • 34
  • 3
    It's a problem with the Chrome console, not your code. You cannot reliably "print" the state of an object. See [this question](http://stackoverflow.com/questions/7377853/apparently-contradictory-behavior-of-javascript-function). – Felix Kling Nov 04 '11 at 09:55
  • That's a bug/problem with the Chrome console as Felix said. But you can write your own log function: http://jsfiddle.net/sNzKq/3/ – ComFreek Nov 04 '11 at 10:09
  • Ok Felix and ComFreek are right, this is a chrome console problem. Is there an open issue for chrome about that ? Thanks – Quentin Nov 04 '11 at 10:14
  • Found only an unconfirmed bug report: https://bugs.webkit.org/show_bug.cgi?id=35801 – Felix Kling Nov 04 '11 at 10:16

1 Answers1

1

Chrome does not clone an object when displaying it in the console, so it will always show the current state. To log the state at a certain position, use e.g. jQuery's $.extend({}, yourObject) to log a clone.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636