繁体   English   中英

在类装饰器中访问此上下文

[英]access this context in class decorator

我的装饰

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 .....");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM