As we already know one of the differences between array and object is:
"If you want to supply specific keys, the only choice is an object. If you don't care about the keys, an array it is" (Read more here)
Besides, according to MDN's docs says that
Arrays
cannot use strings as element indexes(as in an associative array) but must use integers
However, I was so surprised that
var array = ["hello"]; // Key is numeric index
array["hi"] = "weird"; // Key is string
>> content structure looks like: ["hello", hi: "weird"]
The content structure of the array looks so weird. Even more, when I check the type of array it returns true
Array.isArray(array) // true
Question:
- Why have this behavior? This seems inconsistent, right?
- What is the data structure actually storing behind the scene: as an
arrayor something likeobject,hashtable,linkedlist? - Whether this behavior depends on a specific Javascript Engine (V8, SpiderMonkey, etc..) or not?
- Should I use
arraylike this(keys are bothnumeric indexandstring) over normalobject?
from Why does array allow string as an index in JavaScript
No comments:
Post a Comment