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, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15636245.html
未经授权禁止转载,违者必究!