xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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'...
  };
}





应用场景

  1. logger

  2. rewrite function

  3. function overloads

js 方法重写/ 方法重载

js 方法覆写

ts 方法重写/ 方法重载

ts 方法覆写



refs



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-02-24 14:15  xgqfrms  阅读(46)  评论(3编辑  收藏  举报