我的装饰器
function vote(target) {
target.vote = (val = '') => {
console.log(target.name + val);
}
}
class Cat {
@vote
name = 'britanik';
meow() { return `${this.name} says Meow!`}
}
let garfield = new Cat();
garfield.vote('please .....');
但是 target.name 未定义 为什么?
最佳答案
将 target.vote 设为普通函数,它应该可以正常工作。
function vote(target) {
target.vote = function(val=""){
console.log(this.name + val);
};
}
class Cat {
@vote name = "britanik";
meow() {
return `${this.name} says Meow!`;
}
}
let garfield = new Cat();
garfield.vote("please .....");
关于javascript - 在类装饰器中访问此上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51891302/