6

There are a lot of mentions on differentes readings that arrays are a special class of object in Javascript. For example here:

https://www.codingame.com/playgrounds/6181/javascript-arrays---tips-tricks-and-examples

So, and since an object is a collection of properties (or keys) and values, I was thinking if there is a way to start with an object and ends with an array (in the sense that the method Array.isArray() returns true for that object emulating an array). I have started looking at the arrays properties:

let arr = [0, 1, 2, 3, 4, 5];
console.log(Object.getOwnPropertyNames(arr));
console.log(Array.isArray(arr));

So I tried to emulate the same using an object:

let arrEmulation = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, "length":6};
console.log(Object.getOwnPropertyNames(arrEmulation));
console.log(Array.isArray(arrEmulation));

But Array.isArray(arrEmulation) still returns false. First, I want to apologize if this is an stupid question, but is there any way I can manually convert an object to array adding special properties (or keys) to it?

Please, note I don't want to know how to convert object to array, I just want to understand which are those special things that make possible to interpret an object like an array.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
SmaGal
  • 135
  • 9
  • 1
    Possible duplicate of [How to convert an Object {} to an Array \[\] of key-value pairs in JavaScript](https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript) – jmargolisvt Feb 05 '19 at 03:57
  • 7
    I don't believe the suggested duplicate addresses this portion of OP's question: *"I just want to understand which are those special things that make possible to interpret an object like an array."* OP, perhaps [this MIT article](http://web.mit.edu/jwalden/www/isArray.html) might be of use. It's the one referenced in MDN's own [`isArray()` documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Description)... though I'm not sure it really hits the detail you're looking for. – Tyler Roper Feb 05 '19 at 04:00
  • 1
    @jmargolisvt I don't want to know how to convert an object to array, I want to undesrtand what are the properties or keys that I need to add to an object so they are interpreted like an array. – SmaGal Feb 05 '19 at 04:01
  • @TylerRoper thanks, I'm going to give it a read... – SmaGal Feb 05 '19 at 04:03
  • 1
    Looking at the MDN polyfill (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) perhaps you can try getting `Object.prototype.toString.call(arrEmulation)` to return `[object Array]`. I'm not sure what the definition of that function is though. – david Feb 05 '19 at 04:20

3 Answers3

6

I don't think it's possible, in the strictest sense, given the standard specification. Looking up Array.isArray:

If the value of the [[Class]] internal property of arg is "Array", then return true.

So, for Array.isArray(arrEmulation) to return true, you must somehow modify the [[Class]] of the object to be Array, rather than Object. But, looking at ES5's 8.6.2 Object Internal Properties and Methods regarding [[Class]]:

Note: This specification defines no ECMAScript language operators or built-in functions that permit a program to modify an object’s [[Class]] or [[Prototype]] internal properties or to change the value of [[Extensible]] from false to true. Implementation specific extensions that modify [[Class]], [[Prototype]] or [[Extensible]] must not violate the invariants defined in the preceding paragraph.

Also:

Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString

So, the official specification doesn't provide a way to do it in ES5 - if there was a way to do it, it would be non-standard and implementation dependent.

That said, unless you absolutely need to use Array.isArray or have Object.prototype.toString.call(arrEmulation) to return [object Array], you can still use Object.setPrototypeOf to set the prototype of arrEmulation to Array.prototype, allowing you to use array methods on the object and have instanceof Array return true:

const arrEmulation = {0:0, 1:1, 2:2, "length":6};
Object.setPrototypeOf(arrEmulation, Array.prototype);

console.log(arrEmulation instanceof Array);
arrEmulation.forEach((value) => {
  console.log(value);
});
// Internal [[Class]] property is still `Object`, though:
console.log(Object.prototype.toString.call(arrEmulation));
// Unlike a true array:
console.log(Object.prototype.toString.call([]));

console.log('-----');

// although you can set the `toStringTag` to the string 'Array' in ES6+,
// it is cosmetic only and does not pass an `Array.isArray` test:
arrEmulation[Symbol.toStringTag] = 'Array';
console.log(Object.prototype.toString.call(arrEmulation));
console.log(Array.isArray(arrEmulation));

But note that you should avoid using Object.setPrototypeOf in real code:

Warning: Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, a very slow operation, in every browser and JavaScript engine. The effects on performance of altering inheritance are subtle and far-flung, and are not limited to simply the time spent in the Object.setPrototypeOf(...) statement, but may extend to any code that has access to any object whose [[Prototype]] has been altered. If you care about performance you should avoid setting the [[Prototype]] of an object. Instead, create a new object with the desired [[Prototype]] using Object.create().

(of course, Object.create involves creating a new object, which is different from what you want to do, which is to change the existing arrEmulation object)

There doesn't look to be a way to do it in ES6+ either - its text is somewhat similar, but not identical. Specifically, for Array.isArray to return true, the object in question needs to be an "Array exotic object" (or a Proxy that points to one) - but setPrototypeOf only sets the prototype, neither it nor any other method can make the object actually become an Array exotic object (which looks like it has to be natively constructed by the interpreter, and is not emulatable enough).

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Thanks dude, this information gives me some more clarity to understand the differences between and standard object and an array. Just to you know, I'm not really looking to emulate an array with an object, but just to understand they differences and this helps me. +1! – SmaGal Feb 05 '19 at 04:35
  • 1
    @SmaGal the implementation in a typical JavaScript engine will have further optimizations built in. Most likely a typical array will be a densely packed block in memory with direct access via pointers. The same is true for fairly static objects, whose attributes will be replaced by access to memory-addresses to a densely packed in memory struct. Runtime-intensive methods will be compiled to fast memory access via direct pointers in both cases. – Falco Feb 05 '19 at 12:13
2

Javascript is all about prototypal inheritance:

Prototype Inheritance All JavaScript objects inherit properties and methods from a prototype:

Date objects inherit from Date.prototype Array objects inherit from Array.prototype Person objects inherit from Person.prototype The Object.prototype is on the top of the prototype inheritance chain:

Date objects, Array objects, and Person objects inherit from Object.prototype.

As seen here isArray is an function in prototype chain of the Array object.

An polyfill as suggested in MDN Array.isArray() alternate if isArray is not present is:

if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}

So the type is determined by the prototype chain instead of what value it returns.

Similarly, as per Tio Zed's answer

const newArray = Array.from(arrEmulation) // [0, 1, 2, 3, 4, 5]
Array.isArray(newArray)

What it really does is just change to prototype from that of object to that of an Array.

A deeper go through of isArray thanks @Kaiido for making me dig deeper. The Array is array checks these there points

If Type(arg) is not Object, return false. If the value of the [[Class]] internal property of arg is "Array", then return true. Return false.

And

Array instances inherit properties from the Array prototype object and their [[Class]] internal property value is "Array". Array instances also have the following properties.

Black Mamba
  • 13,632
  • 6
  • 82
  • 105
  • Hum... nope. That's not what `Array.isArray` checks. That is a simple polyfill posted on an MDN article, it is in no way how any browser will implement it. Here is [what the specs asks](https://tc39.github.io/ecma262/#sec-isarray) though they're not clear as to how the check is made to know if it is an Array exotic Object. And here is a proof that it's not how it is implemented: https://jsfiddle.net/f6182mkh/ – Kaiido Feb 05 '19 at 05:26
  • I edited my answer I just wanted to show that it all depends on prototypes and not the keys they see in the value. BTW thanks for correcting me. Please edit my answer if you still feel something wrong with my answer. @Kaiido – Black Mamba Feb 05 '19 at 05:32
  • Well even the prototype thing is not correct either: https://jsfiddle.net/vLrgdp86/ – Kaiido Feb 05 '19 at 05:36
  • The above I defined is currently used as a Polyfill in many browsers that doesn't support it. I've searched about how it is currently officially implemented but got no success. It'll be helpful if you can provide me a doc for it. Here's a little detail about the [Array.isArray](http://web.mit.edu/jwalden/www/isArray.html) I've been able to find. [A official doc](https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.5) is here showing how prototypes are implemented for defining array @Kaiido – Black Mamba Feb 05 '19 at 06:19
-2

You can convert anything that is close enough to an array by using Array.from(). In your example, we could just call:

const arrEmulation = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, length: 6};

const newArray = Array.from(arrEmulation) // [0, 1, 2, 3, 4, 5]
Array.isArray(newArray) // true
Tio Zed
  • 25
  • 4
  • 1
    I'm not looking at how to convert object to array, but to understand they differences... – SmaGal Feb 05 '19 at 04:40
  • 2
    @SmaGal, be fair - do you not say 'convert' in the question title? –  Feb 05 '19 at 05:07
  • @eric99 the down-vote don't come from me. I just explained that I was not looking for that. – SmaGal Feb 05 '19 at 05:37
  • @SmaGal my comment was a bit tongue-in-cheek. TioZed deserves some credit for his answer, given the question has now morphed into something more specific. –  Feb 05 '19 at 05:43