Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!