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

102
Views
Javascript arrow func in subclass cannot be found by super class

What I would like to achieve: Essentially, I would like my subclass to have a lexically-bound this function. However, I would like the super class to check that the subclass has an instantiation of this lexically-bound function.

This is how I would prefer to write the code, but it doesn't work:

class Animal {
  constructor(type) {
    this.animalType = type;

    if (!(this.bark instanceof Function)) {
      throw new Error('Found no bark');
    }

  }
}

class Dog extends Animal {

  bark = () => {
    console.log('woof');
  }
}

let max = new Dog('dog')
max.bark();

Yet this works:

class Animal {
  constructor(type) {
    this.animalType = type;

    if (!(this.bark instanceof Function)) {
      throw new Error('Found no bark');
    }

  }
}

class Dog extends Animal {}

Dog.prototype.bark = () => {
  console.log('woof');
}

let max = new Dog('dog')
max.bark();

and this works:

class Animal {
  constructor(type) {
    this.animalType = type;

    if (!(this.bark instanceof Function)) {
      throw new Error('Found no bark');
    }
  }

  bark = () => {
    console.log('woof');
  }
}

class Dog extends Animal {}

let max = new Dog('dog')
max.bark();

Could someone please explain why my first example is failing. It seems to me that bark() isn't in the prototype chain somehow but I'm not sure why.

about 4 years ago · Juan Pablo Isaza
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!