+ 1

В чем заключается разница между массивом и объектом в Javascript?

В чем заключается разница между массивом и объектом в Javascript?

19th Aug 2017, 9:09 AM
davidthegreat
davidthegreat - avatar
3 Antworten
+ 4
Translation↓ What is the difference between an array and an object in Javascript?
19th Aug 2017, 9:14 AM
Ekansh
+ 3
Virtually everything in javascript is an object, so you can "abuse" an Array object by setting arbitrary properties on it. This should be considered harmful though. Arrays are for numerically indexed data - for non-numeric keys, use an Object. Here's a more concrete example why non-numeric keys don't "fit" an Array: var myArray = Array(); myArray['A'] = "Athens"; myArray['B'] = "Berlin"; alert(myArray.length); This won't display '2', but '0' - effectively, no elements have been added to the array, just some new properties added to the array object.
19th Aug 2017, 9:17 AM
Ekansh
0
yes
19th Aug 2017, 9:15 AM
davidthegreat
davidthegreat - avatar