Re:Fun with JavaScript Arrays
by cema on Wednesday June 12, 2013 @09:53AM
(#5868047)
Not just the length property, but also a special array API (push, pop, etc). So there is no equivalence between an array and the simple object constructed from its keys and values, but the object would correctly represent a snapshot of the subset of array key-value pairs.
[ Parent
| on HN ]
Technically, the special API is because arrays extend Array.prototype. You could create an object that does the same, and push(), pop(), etc would work.*
Try going to http://www.objectplayground.com and putting in the following code, selecting the "Show built-in objects" option, and evaluating:
this.array = [];
this.object = Object.create(Array.prototype);
this.object.push("foo");
this.object.push("bar");
*Well, they sort of work. They use and modify the "length" property, so you could see some weird behavior under some circumstances. Not recommended for production use. :-)