Создать метод в конструкторе?
- Как можно создать функцию (метод ) в методе конструктор ?
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');
};
}