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

154
Vistas
check if class has method in javascript

How to check the existence of move() and run() methods shortly and safely? hasOwnProperty returns false of course for both move and run. dog.prototype is undefined so this isn't a way to start.

class Animal {
  constructor(name) {
    this.name = name;
  }
  speak() {
    console.log(`${this.name} makes a noise.`);
  }
  move() // this method may exist or not
  {
      console.log(`${this.name} moves over.`);
  }
}
class Dog extends Animal {
  constructor(name) {
    super(name); 
  }
  speak() {
    console.log(`${this.name} barks.`);
  }
  run() // this method may exist or not
  {
      console.log(`${this.name} runs fast.`);
  }
}
var dog = new Dog('Caesar');
// in the real-world situation we don't know the type of "dog" here.

How to check if dog.run() exists and invokable? How to check if dog.move() exists and invokable?

Edit: The marked-for-duplicate Q/A (Check whether class constructor defines method) does not answer my question.

The final comment-note in my original post is important: in the real-world situation we don't know the type of "dog" So, I cannot write Animal.prototype or Dog.prototype because I don't know Animal and/or Dog. All I have is

  • a reference to an object (dog),
  • knowing that dog is created with a new ClassName statement where ClassName is a class defined by the class-syntax.
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