8

See this jsbin where, to answer another question, I build an array-like object :

function myCollection() {
  var items = [], r = {}
  function myPush(value){
      value += 'bar'
      r[items.length]=value;
      items.push(value)    
  }
  Object.defineProperty(r, "splice", {value:[].splice});
  Object.defineProperty(r, "slice", {value:[].slice});
  Object.defineProperty(r, "length", {
    get : function(){ return items.length}
  });
  Object.defineProperty(r, "myPush", {value:myPush});
  return r;
}

var fooCollection = myCollection();
fooCollection.myPush('foo');
console.log(fooCollection); // logs ["foobar"] 
fooCollection.myPush('Ba');
console.log(fooCollection); // logs ["foobar", "Babar"] 

fooCollection.myPush('wzouing');
console.log(fooCollection.slice(-2)); //logs ["Babar", "wzouingbar"] 
console.log(fooCollection[1]); // logs Babar 

If you hit the Run with JS button on Chromium with the console open, you get this :

enter image description here

The very curious thing is that if you hit the button while the console is closed you get this (you see it after having reopened the console, of course) :

enter image description here

Is that a known bug ? A feature ? A grey zone ? Is there a workaround ?

*Note : sometimes, on Chrome/linux (not Chromium) I get something weirder : existing logs are changed when closing and reopening the console. They can go from the array like form to the folded form. *

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 11
    I think you'd get better responses on chrome's bug-tracker/mailing list. – sabof Feb 27 '14 at 14:37
  • 1
    This question appears to be off-topic because it is about software behavior and not actual programming – SergeS Mar 03 '14 at 08:15
  • Can you kindly let me know what is the expected output. – Ajay Chauhan Apr 30 '14 at 08:58
  • The reason why it always logs `Object[3]` (instead of `Object[1]`/`Object[2]` when `fooCollection` was smaller) is explained [here](http://stackoverflow.com/q/23392111/1048572) – Bergi Jun 09 '14 at 14:00
  • 2
    This question appears to be off-topic because it is about the very specific unspecified behavior of a browser in a rare case. – Denys Séguret Jun 09 '14 at 14:04

3 Answers3

2

I suppose this behaviour could be an intended one to save memory/performance, since resolving/printing runtime objects is a pretty expensive task.

If you really need an unwrapped info, I'd suggest using

console.dir( myStuff );

Or, alternatively, for arrays there is a new

console.table( myArray );

TIP: console.dir is is also the way to log the object contents in Internet Explorer, as opposed to it's default log() output of [object Object].

Ingmars
  • 998
  • 5
  • 10
0

It's more like an issue at: https://bugs.webkit.org/show_bug.cgi?id=35801

Seems it's already fixed in webkit.

Cylon
  • 393
  • 3
  • 11
  • That rather sounds like [bug #35316](http://code.google.com/p/chromium/issues/detail?id=50316)? This question asks about logging when the whole *console* is closed, not about modifying objects that have been logged (while the console was open) – Bergi Jun 09 '14 at 13:56
0

The very curious thing is that if you log an object while the console is closed, you don't get the expanded (array-like) view (after having reopened the console, of course).

Is that a known bug? A feature?

A feature, I'd guess. The console is optimized for performance while it is not opened, and therefore does not compute that expanded view of the object

Is there a workaround?

Have a look at How can I change the default behavior of console.log? (*Error console in safari, no add-on*). You'd have the typical lazyness problem anyway. Logging JSON strings should help.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375