Создать метод в конструкторе?

  1. Как можно создать функцию (метод ) в методе конструктор ?

class User {

  constructor(name) {
    this.name = name;
  sayHi() {
    alert('hello');
  }
  }

  

}

// Использование:
let user = new User("Иван");
user.sayHi();


Ответы (2 шт):

Автор решения: meine

class A {
  constructor(name) {
    this.name = name;
    
    this.f = function() {
      console.log(this.name);
    }
  }
}

const a = new A('name');

a.f();

→ Ссылка
Автор решения: artomich
constructor(name) {
    this.name = name;
    this.sayHi = () => {
       alert('hello');
     };
  }
→ Ссылка