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

TypeScript Reflect.getMetadata & Reflect.defineMetadata All In One

TypeScript Reflect.getMetadata & Reflect.defineMetadata All In One

$ npm i reflect-metadata

# $ npm i -S reflect-metadata
# $ npm i --save reflect-metadata

https://www.npmjs.com/package/reflect-metadata

https://github.com/rbuckton/reflect-metadata


export function controller(target: any) {
  for (const key in target.prototype) {
    // Object.prototype.hasOwnProperty()
    console.log('key', key);
    Reflect.getMetadata('path', target.prototype, key);
    // Property 'getMetadata' does not exist on type 'typeof Reflect'.ts(2339)
  }
}

export function get(path: string) {
  return function (target: any, key: string) {
    Reflect.defineMetadata('path', path, target.prototype, key);
    // Reflect.defineProperty(target, propertyKey, attributes);
  }
}

export function post(target: any) {
  //
}

export function put(target: any) {
  //
}

// Identifier expected. 'delete' is a reserved word that cannot be used here.ts(1359)
// export function delete(target: any) {
//   //
// }
export function Delete(target: any) {
  //
}


Reflect

The static Reflect.defineProperty() method is like Object.defineProperty() but returns a Boolean.

静态 Reflect.defineProperty() 方法类似于 Object.defineProperty() 但返回一个布尔值。

Reflect.defineProperty(target, propertyKey, attributes);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/defineProperty

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

refs



©xgqfrms 2012-2020

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

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


posted @ 2022-04-16 15:18  xgqfrms  阅读(389)  评论(4编辑  收藏  举报