Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
Javascript, extending class with Array with iterator support

I found a very weird behavior, I'm using Firefox 93.0 64 bits, lets pick the next example:

class SubArray extends Array{
 constructor(...i){
  super(...i)
 }
}

The intuitive behavior, is that this new class should do the same as a normal array, with all their properties, but does not do that, we get the next in the console.

a=new SubArray([1, 2, 3])
a[0]
Array(3) [ 1, 2, 3 ]

We are not getting the first element....

How can we extends a class to array, and appropriately inherit properties? (all of them, set elements, get elements, etc, etc)

Thx,

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

as @Barmar says.

This will works if we do new SubArray(1, 2, 3, 4), this is because there is a difference betwen the builtin classes and the user classes, in the builtin classes we use [1, 2, 3], we are not using the normal user constructor, so the equivalence would be new Array(1, 2, 3).

Just to keep in mind, if we are using builtin classes from JS, we need to check how JS translate the usual form like [] to the usual constructor, other example would be, how {1: 'hi'} is translated to the Object constructor?, just keep in mind this question.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda