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

89
Views
How to find if a method belongs to super or - Extending Array and Testing with ES6

I'm new to OOP in ES6 and trying to understand how to extend Array so i can create pipelines. I'm starting with a simple case of extending Array ad overriding Array.filter, and expecting result.hasOwnProperty("filter") to be true... but it's not:

So how do you test that the method filter now lives in the Wordlist class?

export class Wordlist extends Array{
  constructor(...args){
    super(...args)
  }

  filter(...args){
    console.log("filtering")
    return super.filter(...args)
  }
}

// test
describe.only('Wordlist.filter', ()=>{
  it('should filter an array', ()=>{
    const wordlist = new Wordlist('hello', 'rhubarb')
    const result = wordlist.filter(word => word === 'rhubarb')

    expect(result.length).toEqual(1) // pass
    expect(result).toEqual(["rhubarb"]) // pass
    expect(result.hasOwnProperty('filter')).toBe(true) // should pass but doesnt!!! 
    expect(result.hasOwnProperty('reduce')).toBe(false) // passes
  })
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The misunderstanding is about how .hasOwnProperty() works. This returns a boolean that indicates whether an object directly contains a specific property. In your case, you are defining a property for the class which puts the new .filter() property on the object's prototype which means that the .filter() property is NOT directly on the object, but is inherited from the object's prototype.

So, by definition when the property is only on the prototype, .hasOwnProperty() will return false.

If you want to see if result is actually a Wordlist object, then you can use:

 result instanceof Wordlist   // should be true

to test for that. You could also do this:

 Object.getPrototypeOf(result).hasOwnProperty('filter')   // should be true

And, to make sure it's not the same .filter as an array has, you could check this:

result.filter !== [].filter   // should be false
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!