# 在主题和插件中迁移到JS原生类语法

**URL:** <https://meta.discourse.org/t/moving-to-js-native-class-syntax-in-themes-and-plugins/356588>\
**Category:** Development\
**Tags:** dev-news\
**Created:** [2025年三月10日 11:11 UTC](https://meta.discourse.org/t/moving-to-js-native-class-syntax-in-themes-and-plugins/356588 "2025-03-10T11:11:58Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![david](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/david/32/157490_2.png) [@david](https://meta.discourse.org/u/david)\
**Post date:** [2025年三月10日 11:11 UTC](https://meta.discourse.org/t/moving-to-js-native-class-syntax-in-themes-and-plugins/356588/1 "2025-03-10T11:11:58Z")

</div>

在过去的几年里，我们一直在从 Ember 的旧版 `.extend({ ... })` 语法转向原生类语法，例如 `class extends Foo {...}`。

虽然旧版语法在技术上仍然受支持，但现代 JavaScript 工具（包括 IDE 和 Prettier 3.0）不支持基于旧版对象字面量的语法中的装饰器。在我们推荐的最新版 linting 配置中，使用旧版语法时会看到以下错误：

**ESLint** : `error Native JS classes should be used instead of classic classes ember/no-classic-classes`

**Prettier** : `SyntaxError: Decorators cannot be used to decorate object literal properties`

因此，如果您还没有这样做，现在是时候将您的主题和插件代码更新到新语法了。主要需要更新两处：新的类定义和 `modifyClass` 调用。

## 类定义

大部分工作可以使用 [ember-native-class-codemod](https://github.com/ember-codemods/ember-native-class-codemod) 来完成。在您的主题/插件存储库中运行：

`NO_TELEMETRY=true npx ember-native-class-codemod@4.1.1 --no-classic-decorator **/*.js`

这将遍历您的文件并将它们转换为新的原生类语法。有时它会提示您手动干预。请务必审查它所做的代码更改——它并非完美无缺。

如果您有许多主题/插件需要更新，您可能会对[我们的 mass-pr 脚本](https://github.com/discourse/mass-pr/blob/main/scripts/js-native-class.rb)感兴趣。

## modifyClass 调用

如果您在 `modifyClass` 调用中使用装饰器，那么您需要使用新的基于原生类的语法。有关更多信息，请参阅[文档](https://meta.discourse.org/t/using-modifyclass-to-change-core-behavior/262064)。在进行这些更改时，请牢记记录的限制。

---

_[View the full topic](https://meta.discourse.org/t/moving-to-js-native-class-syntax-in-themes-and-plugins/356588)._
