Answer the question
In order to leave comments, you need to log in
How to dynamically add a method to a class, inside the same class?
I want to dynamically create new methods in one of the class methods, the names of which are also created in the process of work.
initDOMListeners() {
this.listeners.forEach(list => {
const method = capitalize(list)
this[method] = this[method].bind(this)
this.$root.on(list, this[method])
})
}
Answer the question
In order to leave comments, you need to log in
In typescript, you cannot change an existing class. A class is a complete entity. End.
With inheritance, you can add new methods to a new derived class .
And of course, you can hack anything that is possible in javascript, forcibly bypassing ts, but ts will not know about it and, accordingly, no typing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question