predefined iterables: implications with the length property and the access operator []


I was wondering if all the iterables predefined in JavaScript that have the property .length , can access their elements via operator [<indice numérico>] and vice versa?

Array -> '.length' & operador '[]'
String -> '.length' & operador '[]'
TypedArray -> '.length' & operador '[]'
Set -> no tiene '.length' & no accede a los elementos iterados via '[]'
Map -> no tiene '.length' & no accede a los elementos iterados via '[]'
HTMLCollection -> '.length' & operador '[]'

I find that they all have length, you can access their iterated elements via the operator [] and vice versa. I ask if there are any exceptions.

asked by xgbuils 18.05.2017 в 19:57

1 answer


Considering length is a property of Object , [] can be used on all indexable objects, so on exceptions to the possibility of using length and [] on indexable objects, There are not any.

Several of the included objects are global objects but others are not, and some are ECMAScript 6 indexed objects and others are not. You can check the list of global objects in various sites on JavaScript, for example the page Global Objects from Mozilla Developer Network, or directly in the specification ECMAScript 6 .

About the objects mentioned in the question

  • Array is a global object of the indexed collection type.
  • String is a global object of type text processing.
  • TypedArray is not a global object but it is an object of type indexed collection
  • Set and Map are global objects of the collection type with key
  • HTMLCollection is not part of ECMASCript 6, it is an API.

Other global indexable objects are WeakMap and WeakSet.

answered by 20.05.2017 в 00:08