Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda