# 重新开放模型类的原因是什么？

**URL:** https://meta.discourse.org/t/what-is-the-reason-behind-reopening-the-model-class/129627
**Category:** Development
**Created:** [2019 年9 月 27 日 01:49 UTC](https://meta.discourse.org/t/what-is-the-reason-behind-reopening-the-model-class/129627 "2019-09-27T01:49:40Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![rimian](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rimian/32/120658_2.png) [@rimian](https://meta.discourse.org/u/rimian)
#### Post date: [2019 年9 月 27 日 01:49 UTC](https://meta.discourse.org/t/what-is-the-reason-behind-reopening-the-model-class/129627/1 "2019-09-27T01:49:40Z")

</div>

我注意到备份模型在同一个文件中先定义了一个模型，然后在几行之后重新打开它，添加了如 find、start、cancel 等功能。我想知道这样设计的原因是什么？为什么需要重新打开这个类？我相信这肯定有合理的理由，只是我还不了解。 🙂

```
const Backup = Discourse.Model.extend({
  destroy() {},
  restore() {}
});

Backup.reopenClass({
  find() {
  ...
  }
  // 等等
});

```

参见：[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/admin/models/backup.js.es6](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/admin/models/backup.js.es6)

---

<div class="post-metadata">

### Author: ![xrav3nz](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/xrav3nz/32/76894_2.png) [@xrav3nz](https://meta.discourse.org/u/xrav3nz)
#### Post date: [2019 年9 月 27 日 02:53 UTC](https://meta.discourse.org/t/what-is-the-reason-behind-reopening-the-model-class/129627/2 "2019-09-27T02:53:29Z")

</div>

好问题，我之前也对这个有点困惑！

`reopenClass` 用于向类本身添加 **静态** 方法（或属性）。我认为无法通过 `Ember.Object.extend` 来定义静态方法。

关于 `reopenClass` 和 `reopen` 的更多信息：[https://guides.emberjs.com/release/object-model/reopening-classes-and-instances/](https://guides.emberjs.com/release/object-model/reopening-classes-and-instances/)
