为什么 class 中的这个方法没有扩展到下一个 class,相反我在 TypeScript 中看到 - [Function (anonymous)]

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

register() 与 Person class 一起工作正常,当我在 Person class

之后 console.log 时,我确实看到了 return 语句

稍后我将它扩展到我的员工 class 但是当我 console.log 它在那里时,我看到 - [Function (anonymous)] 而不是我的 return 语句已设置

这是代码:

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);

这就是我在终端中看到的 console.log

[Function (anonymous)]

似乎方法没有正确扩展。 我该如何解决这个问题 - 目标是看到 return 语句与它在 Person class

中的工作方式相同

现在您正在控制台记录

console.log(emp.register);

其中 return 个:

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

又名函数,如果你添加括号

console.log(emp.register());

会return

"Shawn is now registered"