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

213
Vistas
Why is this method in a class is not getting extended to the next class, instead I see - [Function (anonymous)] in TypeScript

register() works fine with the Person class and I do see the return statement when I console.log it after the Person class

Later on I am extending it to my Employee class but when I console.log it there, I see - [Function (anonymous)] instead of the return statement which I have set

here is the code :

interface PersonInterface {
  id: number;
  name: string;
  register(): string;
}

// Classes
class Person implements PersonInterface {
  id: number; 
  name: string;

  constructor(id: number, name: string) {
    this.id = id;
    this.name = name;
  }

  register() {
    return `${this.name} is now registered`;
  }
}

const mike = new Person(2, "Mike Jordan");

// console.log(mike.register()); - I get 'Mike Jordan is now registered'
   
class Employee extends Person {
  position: string;

  constructor(id: number, name: string, position: string) {
    super(id, name);
    this.position = position;
  }
}

const emp = new Employee(3, "Shawn", "Developer");

console.log(emp.register);

and this is what I see in the terminal for that console.log

[Function (anonymous)]

It seems like the method is not extended properly. How can I resolve this - the goal is to see the return statement just the same way that it works within the Person class

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Now you are console logging

console.log(emp.register);

which returns:

register() {
    return `${this.name} is now registered`;
} 

aka a function, if you add parenthesis

console.log(emp.register());

it will return

"Shawn is now registered"
about 4 years ago · Juan Pablo Isaza Denunciar
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