TypeScript @decorator All In One
TypeScript @decorator All In One
修饰器,装饰器
https://www.typescriptlang.org/docs/handbook/decorators.html
function f() {
console.log("f(): evaluated");
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor
) {
console.log("f(): called");
};
}
function g() {
console.log("g(): evaluated");
return function (
target,
propertyKey: string,
descriptor: PropertyDescriptor
) {
console.log("g(): called");
};
}
class C {
@f()
@g()
method() {}
}
// Which would print this output to the console:
f(): evaluated
g(): evaluated
g(): called
f(): called
decorator factories
https://www.typescriptlang.org/docs/handbook/decorators.html#decorator-factories
function color(value: string) {
// this is the decorator factory
return function (target) {
// this is the decorator
// do something with 'target' and 'value'...
};
}
应用场景
-
logger
-
rewrite function
-
function overloads
js 方法重写/ 方法重载
js 方法覆写
ts 方法重写/ 方法重载
ts 方法覆写
refs
©xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/14439070.html
未经授权禁止转载,违者必究!