tsconfig的useDefineForClassFields属性怎么使用

这篇“tsconfig的useDefineForClassFields属性怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“tsconfig的useDefineForClassFields属性怎么使用”文章吧。

创新互联主营思南网站建设的网络公司,主营网站建设方案,成都app开发,思南h5重庆小程序开发公司搭建,思南网站营销推广欢迎思南等地区企业咨询

useDefineForClassFields 作用

class 声明中的字段语义从 [[Set]] 变更到 [[Define]]

示例

如下代码

export class C {
  foo = 100;
  bar: string;
}

useDefineForClassFields 设置为 false 时,编译后结果如下

export class C {
  constructor () {
    this.foo = 100;
  }
}

useDefineForClassFields 设置为 true 时,编译后结果如下, 要注意 target 版本要在 ES2021 之前的版本

export class C {
  constructor () {
    Object.defineProperty(this, 'foo', {
      enumerable: true,
      configurable: true,
      writable: true,
      value: 100
    })
    Object.defineProperty(this, 'bar', {
      enumerable: true,
      configurable: true,
      writable: true,
      value: void 0
    })
  }
}

target 版本在 ES2022 及其以上,编译结果如下

export class C {
  foo = 100;
  bar;
}

以上就是关于“tsconfig的useDefineForClassFields属性怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注创新互联行业资讯频道。


网页名称:tsconfig的useDefineForClassFields属性怎么使用
新闻来源:http://myzitong.com/article/jedpgh.html