Answer the question
In order to leave comments, you need to log in
Why doesn't the decorator remove the property?
I instantiate a class and use a decorator to try to remove a property:
function format(target: Object, propertyKey: string) {
delete this[propertyKey]
}
class User {
@format
name: string
constructor(name: string) {
this.name = name
}
print(): void {
console.log(this.name)
}
}
let tom = new User('Tom')
tom.print()
Answer the question
In order to leave comments, you need to log in
You cannot access this in a decorator method, because at the moment when the logic that you have included in this method is working out, by definition there are no class instances, and hence the this contexts of these instances. In your example, you are trying to remove a property on the propertyKey string from a Window.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question