Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array Like Objects in Javascript

I'm wondering how jQuery constructs its array-like object. The key thing I'm trying to work out is how it manages to get the console to interpret it as an array and display it as such. I know it has something to do with the length property, but after playing a bit I can't quite figure it out.

I know this has no technical advantage over a normal array like object as in the example below. But I think it's an important semantic element when users are testing and debugging.

A normal Array like Object.

function foo(){     // Array like objects have a length property and it's properties use integer     // based sequential key names, e.g. 0,1,2,3,4,5,6 just like an array.     this.length = 1;     this[0] = 'hello' } // Just to make sure add the length property to the prototype to match the Array  // prototype foo.prototype.length = 0;  // Give the Array like object an Array method to test that it works      foo.prototype.push = Array.prototype.push  // Create an Array like object  var bar = new foo;  //test it  bar.push('world');  console.log(bar); // outputs  { 0: 'hello',   1: 'world',   length: 2,   __proto__: foo } 

Where as jQuery would output

var jQArray = $('div')  console.log(jQArray);  // outputs [<div></div>,<div></div>,<div></div>,<div></div>] 

If you run

console.dir(jQArray)  // Outputs  { 0: HTMLDivElement,   1: HTMLDivElement,   2: HTMLDivElement,   3: HTMLDivElement,   4: HTMLDivElement,   context: HTMLDocument,   length: 5,   __proto__: Object[0]  } 

The proto of the jQuery object is especially interesting since its the Object and not jQuery.fn.init as would be expected, also the [0] indicates something as this is what you get when you.

console.dir([]) // outputs Array[0] as the object name or Array[x] x being the internal length of the // Array 

I have no idea how jQuery has set it's proto to be Object[0] but my guess is that answer lies somewhere in there. Anyone got any ideas?

like image 826
AshHeskes Avatar asked Jul 06 '11 15:07

AshHeskes


People also ask

What is {} and [] in JavaScript?

{} is shorthand for creating an empty object. You can consider this as the base for other object types. Object provides the last link in the prototype chain that can be used by all other objects, such as an Array . [] is shorthand for creating an empty array.

Can you make an array of objects in JavaScript?

Creating an array of objectsWe can represent it as an array this way: let cars = [ { "color": "purple", "type": "minivan", "registration": new Date('2017-01-03'), "capacity": 7 }, { "color": "red", "type": "station wagon", "registration": new Date('2018-03-03'), "capacity": 5 }, { ... }, ... ]

How do you turn an object into an array?

To convert an object to an array you use one of three methods: Object.keys() , Object.values() , and Object.entries() . Note that the Object.keys() method has been available since ECMAScript 2015 or ES6, and the Object.values() and Object.entries() have been available since ECMAScript 2017.

What is difference between array and array object in JavaScript?

Both objects and arrays are considered “special” in JavaScript. Objects represent a special data type that is mutable and can be used to store a collection of data (rather than just a single value). Arrays are a special type of variable that is also mutable and can also be used to store a list of values.


1 Answers

The object has to have length and splice

> var x = {length:2, '0':'foo', '1':'bar', splice:function(){}} > console.log(x); ['foo', 'bar'] 

and FYI, the Object[0] as the prototype is for exactly the same reason. The browser is seeing the prototype itself as an array because:

$.prototype.length == 0; $.prototype.splice == [].splice; 
like image 186
Nobody Avatar answered Oct 05 '22 23:10

Nobody