我正试图将我的头脑包裹在打字稿中的装饰器和元数据中,并且思考角度是一个很好的项目,可以寻找某种最佳实践 .

在旧版本(例如2.1)中,他们使用的是DecoratorFactories,在内部他们将metadta应用于 reflect-metadata API提案(https://github.com/angular/angular/blob/2.1.x/modules/%40angular/core/src/util/decorators.ts

在新版本中,他们开始为装饰器(https://github.com/angular/angular/blob/master/packages/core/src/util/decorators.ts)实现自己的系统,并将数据附加到易于访问的构造函数中 . 喜欢:

@SomeDecorator(someDataObject)
class SomeClass { ..... }

console.log(SomeClass['__annotations__'])
console.log(SomeClass['__paramaters__'])
console.log(SomeClass['__prop__metadata__'])

有人知道他们为什么要这样做吗?好处?缺点是什么?