Javascript 类 中的“@”符号有什么用?

What is the purpose of the "@" symbol in Javascript classes?

在 AdonisJS 的 this example 中,Post class 定义包含 @column 部分。有人可以解释这是做什么的吗?我假设它在 Post class 中创建 'column' class 的多个实例作为成员变量,每个实例都有不同的名称和数据类型。但这是如何工作的,@ 符号有什么用?

  import { column, BaseModel } from '@ioc:Adonis/Lucid/Orm'

  export default class Post extends BaseModel {
    @column({ isPrimary: true })
    public id: number

    @column()
    public title: string

    @column()
    public description: string
  }

以下是否等效(没有定义的数据类型)?

export default class Post extends BaseModel
{
   constructor()
   {
      this.id = new column({ isPrimary: true });
      this.title = new column();
      this description = new column();
   }
}

更新:

在意识到 AdonisJS 是用 TypeScript 编写后,我找到了 this,这回答了这个问题。

这些被称为“装饰器”,是提议的 javascript 功能。

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

该提案已经经过多次迭代。 TypeScript and babel 两者都有自己的装饰器实现。

装饰器可以修改整个 classes 或 class 字段,例如通过调用 Object.defineProperty(classPrototype, key, {...}).

装饰器具体做什么取决于它的实现功能。