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

js @decorator All In One

js @decorator All In One

Decorators for ES6 classes

https://github.com/tc39/proposal-decorators

function deco (target) {
  target.flag = true;
}

@deco
class Test {
  constructor (name = 'Eric') {
    this.name = name;
  }
  getName () {
    return this.name;
  }
  static readName = 'Eric';
}

Test.name;
// 'Test'
Test.readName;
// 'Eric';
Test.flag;
// 

function deco (target) {
  // 原型链
  target.prototype.flag = true;
}

@deco
class Test {
  constructor (name = 'Eric') {
    this.name = name;
  }
}

const test = new Test();

test.name;
//
test.flag;
// 

TypeScript Decorators

https://www.typescriptlang.org/docs/handbook/decorators.html

Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.ts(1219)
class Test


function deco (target) {
    target.flag = true;
}
@deco
class Test {
    constructor (name = 'Eric') {
        this.name = name;
    }
    getName () {
        return this.name;
    }
    static readName = 'Eric';
}
console.log('@deco', Test.flag);


refs



©xgqfrms 2012-2020

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

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


posted @ 2021-12-03 00:54  xgqfrms  阅读(33)  评论(7编辑  收藏  举报