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

104
Vistas
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
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