Hacker News new | ask | show | jobs
by sheept 7 days ago
You probably meant [object Object] :) Since arrays have their own default toString implementation (its own can of worms that is the basis for JSFuck[0]), you'd have to go out of your way with Object.prototype.toString.call to get [object Array]

[0]: https://jsfuck.com/ relies on Array#toString for casting values to strings

2 comments

I intentionally corrected it, because vanilla JS arrays used to behave like this in some contexts, but I'm not even sure about which ones still.

Since the "Array" is a reference to the prototype, I think it might be outdated due to changes in undefined behavior of runtimes when serializing array objects, or logging them.

I'm pretty sure that [object Array] used to be the result of logging an array at some point.

  Object.prototype.toString
always returns the result of

  Array.prototype.join

per spec, afaik, so for an empty array it's the empty string.
Yep, there are no arrays in JS. There are just objects, that behave like arrays.
https://tc39.es/ecma262/multipage/indexed-collections.html#s...

> Arrays are exotic objects that give special treatment to a certain class of property names. See 10.4.2 for a definition of this special treatment.

I just meant these special properties. The behavior, apart from the square-bracket syntax for construction, can be emulated using Object property descriptors, Symbol.iterator etc, but AFAIK, much of this is retro-fitted.

Not disagreeing with the fact that arrays are almost just regular objects in JS, but the "just" in "just objects" does have nuances, AFAIK.

JIT Optimizations for non-sparse arrays might just be part of a larger hot-path optimization system, but I think there are still differences.

Is it possible to create an object for which

  Array.isArray
returns true without it being instantiated using an array constructor or other array-returning function, the Array prototype, or square-bracket syntax?

E.g.

  Array.from({"0": 1, "1: 2})

  Array.from({length: 2}, (i) => i + 1)

  [1, 2]

  [...Object.values{"a": 1, "b": 2})]
...

all implicitly use the built-in Array prototype.

I'm not sure if it's possible to build an array using only primitives and functions from the

  Object.
namespace, for example.