Note that when you use the position index (or 0), the values โโare placed inside the array:
var array = []; array[0] = "Foo"; array[1] = "Bar";
This is not the case when you add non-index values โโ(not 0-9 +):
var array = []; array[0] = "Foo"; array[1] = "Bar"; array[-1] = "Fizzbuzz";
Values โโare only placed in an array when you play by the rules. When you do not, they will not be accepted. However, they are accepted on an Array object, which is the case with almost anything in JavaScript. Even if ["Foo", "Bar"] are the only values โโin our array, we can still access "Fizzbuzz" :
array[-1];
But note that this is not part of the array values, since its "index" is not valid. Instead, it was added to the array as another member. We could access the other elements of the array in the same way:
array["pop"];
Note that we are accessing the pop method in an array that tells us that it contains native code. We do not refer to any of the values โโof the array with the key "pop", but rather to the element of the array object. We can confirm this again by clicking on the public elements of the object:
for (var prop in array) console.log(prop, array[prop]);
From which the following follows:
0 Foo 1 Bar -1 Fizzbuzz
So again, this is on an object, but it is not in an array.
Awesome question! Thought I should do a double trick.
Sampson Nov 29 '12 at 4:16 2012-11-29 04:16
source share