0
点赞
收藏
分享

微信扫一扫

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();

举报

相关推荐

0 条评论