ts重点学习126-装饰器的小例子笔记

export default {};

/* const userInfo: any = undefined;

class Test {
  getName() {
    try{
      return userInfo.name
    }catch(e) {
      console.log(e);
      
    }
  }

  getAge() {
    try{
      return userInfo.age
    }catch(e) {
      console.log(e);
      
    }
  }
}

const t = new Test()
t.getName()
t.getAge() */

/* 
const userInfo: any = undefined;

function catchErrorDecorator(target: any, key: string, descriptor: PropertyDescriptor) {
  const fn = descriptor.value
  descriptor.value = function() {
    try{
      fn()
    }catch(e) {
      console.log("userInfo上面不存在该属性");
      
    }
  }
}

class Test {
  @catchErrorDecorator
  getName() {
    return userInfo.name
  }
  @catchErrorDecorator
  getAge() {
    return userInfo.age
  }
}

const t = new Test()
t.getName()
t.getAge() */

const userInfo: any = undefined;

function catchErrorDecorator(msg: string) {
  return function (target: any, key: string, descriptor: PropertyDescriptor) {
    const fn = descriptor.value;
    descriptor.value = function () {
      try {
        fn();
      } catch (e) {
        console.log(msg);
      }
    };
  };
}

class Test {
  @catchErrorDecorator("userInfo.name 不存在")
  getName() {
    return userInfo.name;
  }
  @catchErrorDecorator("userInfo.age 不存在")
  getAge() {
    return userInfo.age;
  }
}

const t = new Test();
t.getName();
t.getAge();

posted @ 2022-09-30 20:40  前端导师歌谣  阅读(18)  评论(0)    收藏  举报