Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array-like objects and arrays not logged like arrays if the console is closed

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

like image 676
Denys Séguret Avatar asked Feb 27 '14 14:02

Denys Séguret


People also ask

Which array operations can be done on an array-like object?

We can use the ES6 spread operator([... array-like]) to convert an Array-like to an Array. Let us revisit the example of the arguments. We are using the spread operator on arguments and are now allowed to use forEach on it.

Why do we call arguments an array-like object?

arguments is an array-like object, which means that arguments has a length property and properties indexed from zero, but it doesn't have Array 's built-in methods like forEach() or map() . However, it can be converted to a real Array , using one of slice() , Array.from() , or spread syntax.

How do you make an array-like?

The following code takes a regular object, adds properties to make it an array-like object, and then iterates through the “elements” of the resulting pseudo-array: var a = {}; // Start with a regular empty object // Add properties to make it "array-like" var i = 0 ; while ( i < 10 ) { a [ i ] = i * i ; i ++ ; } a .

How do you access an array of objects?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };


1 Answers

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

like image 150
Ingmars Avatar answered Oct 19 '22 11:10

Ingmars