1. // 普通方法,target 对应的是类的 prototype
    2. // 静态方法,target 对应的是类的构造函数
    3. function getNameDecorator(target: any, key: string, descriptor: PropertyDescriptor) {
    4. // console.log(target, key);
    5. // descriptor.writable = true;
    6. descriptor.writable = false
    7. descriptor.value = function() {
    8. return 'decorator';
    9. };
    10. }
    11. class Test {
    12. name: string;
    13. constructor(name: string) {
    14. this.name = name;
    15. }
    16. @getNameDecorator
    17. getName() {
    18. return this.name;
    19. }
    20. }
    21. const test = new Test('dell');
    22. console.log(test.getName());