简体   繁体   中英

Are JavaScript Array elements nothing more than Array object properties?

I have observed the following:

var o = {}; // empty JS object
var a = []; // empty JS array

o.myproperty = "I am property";
a.push("I am array element");

alert(o['myproperty']); // alerts "I am property"
alert(o.myproperty); // alerts "I am property"
alert(a[0]); // alerts "I am array element"
alert(a['0']); // alerts "I am array element"

/* Of course, accessing an array element using dot notation (e.g. a.0) would cause a
   SyntaxError: Unexpected number (in JavaScript variable names cannot begin with numbers.)
*/

Also:

'myproperty' in o // evaluates to true
0 in a // true
'0' in a // true

delete o.myproperty; // true, o is now empty
delete o['myproperty']; // true, o is now empty
delete a[0]; // true, a contains now 1 undefined element
delete a['0']; // true, a contains now 1 undefined element

It seems as if adding an element to an array actually creates a numbered property on the array object, that is afterwards used to access the element by reference. But some things do not work on these array element properties exactly as they do on normal object properties (an example is the above deletion). So...

TL;DR ... Are javascript array elements accessed by reference via unseen numbered array object properties?

EDIT seems so: Why is using "for...in" with array iteration a bad idea?

Pretty much the only thing an Array adds over an Object is the .length property, and a few array-specific methods like .push . That's really it, under the hood an Array is just an Object . What really makes it usable as an array is said .length parameter; it allows you to iterate the properties in an ordered manner with a for (i = 0; i < arr.length; i++) loop. The .length property is updated when you .push() new elements into the array or when doing some other array-specific manipulations. And that's really all you need to make an object work as an indexed, ordered list.

To make an accessor value an index in the array it should:

  • Be a non-negative integer ( 0 , 1 , 100 , 200 );
  • If converted to a number (from a string ), it should be a non-negative integer ('0', '1.000')

For example:

a['100'] // as index a[100]
a['0']   // as index a[0]
a[1.00]  // as index a[1]

In all other cases the accessor is treated as a property of the array object:

a[-1]       // as a property a['-1']
a['myProp'] // as a property a['myProp']

What makes the array different from regular objects: when using indexes the length property is auto-updated. The length should be always bigger by 1 than the highest index.

Arrays are Objects, the simplest way to see it is [] instanceof Object which returns true .

And yes, Array elements are accessed by reference the same way Object properties are.

When you delete a property of an Object, you are actually setting this property to undefined, so it's the same for an object or an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM