# 移除 Ember 字符串原型扩展

**URL:** https://meta.discourse.org/t/removal-of-ember-string-prototype-extensions/283513
**Category:** Development
**Created:** [2023年十月25日 17:06 UTC](https://meta.discourse.org/t/removal-of-ember-string-prototype-extensions/283513 "2023-10-25T17:06:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2023年十月25日 17:06 UTC](https://meta.discourse.org/t/removal-of-ember-string-prototype-extensions/283513/1 "2023-10-25T17:06:33Z")

</div>

问题是 `@ember/string` 已弃用，似乎是在 Ember 4.x 之后？我找不到 Ember 5.x 中的这个包……所以这只是一个权宜之计，直到 Discourse 快速迁移到 5.x（David 在这里暗示过：[Upgrading Discourse to Ember 4 - #14 by david](https://meta.discourse.org/t/upgrading-discourse-to-ember-4/225032/14?u=merefield%EF%BC%89)

@David 这里的正确做法是什么——使用原生 Javascript（就像我做的那样）？

---

<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: [2023年十月25日 17:39 UTC](https://meta.discourse.org/t/removal-of-ember-string-prototype-extensions/283513/2 "2023-10-25T17:39:42Z")

</div>

感谢您的报告——我们没想到 JQuery 的更改会影响字符串原型扩展，因此我们正在对此进行研究 👀

> [@merefield](#):
>
> 问题是 `@ember/string` 在 Ember 4.x 之后似乎已弃用？

据我所知，只有原型扩展已弃用。因此，像 `\"mystring\".capitalize()` 这样的操作将不允许。

但您仍然可以执行类似的操作：

```js
import { capitalize } from "@ember/string";

capitalize("mystring");

```

因此，我建议您在 topic-list-previews 组件中这样做 👌

（更多之前/之后示例请参见 [此处](https://deprecations.emberjs.com/v3.x/#toc_ember-string-prototype_extensions)）

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2023年十月25日 17:51 UTC](https://meta.discourse.org/t/removal-of-ember-string-prototype-extensions/283513/3 "2023-10-25T17:51:48Z")

</div>

没关系，但对我来说也很意外 😅

---

<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: [2023年十月26日 10:10 UTC](https://meta.discourse.org/t/removal-of-ember-string-prototype-extensions/283513/4 "2023-10-26T10:10:57Z")

</div>

我查看了 `all-the-themes` 和 `all-the-plugins`，只有少数几个公共插件需要修复。我在这里提交了 PR：（抄送 @merefield @angus）

- [Remove use of Ember string prototype extensions by davidtaylorhq · Pull Request #35 · paviliondev/discourse-layouts · GitHub](https://github.com/paviliondev/discourse-layouts/pull/35)

- [Remove use of Ember string prototype extensions by davidtaylorhq · Pull Request #7 · paviliondev/discourse-news · GitHub](https://github.com/paviliondev/discourse-news/pull/7)

Godfrey 帮我们深入研究了 JQuery commit 触发此问题的原因，他的发现都在 [这个 PR](https://github.com/discourse/discourse/pull/24101) 中。因此，我们理论上可以恢复此功能，但这涉及到对上游依赖项应用补丁，这并非理想方案。

考虑到复杂性，以及我们几个月后无论如何都会移除它们，我认为现在直接彻底移除是更合理的。我在这个 PR 中正式提出了这一点：

> <https://github.com/discourse/discourse/pull/24110>
>
> These have been deprecated for some time, and the vast majority of themes/plugin…s have already removed their use. The prototype extensions were unexpectedly disabled as a side effect of 895036bd7a057f2430c5c917040319577fcf6b95 (more details in https://github.com/discourse/discourse/pull/24101).
> 
> Given that restoring the functionality now involves significant complexity, and would only be delaying the inevitable removal in a matter of months, we've decided to keep them disabled. This commit explicitly sets the flag in the ember environment config to make things clearer.

如果您有任何疑问/疑虑，请告知。

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [2023年十月26日 10:12 UTC](https://meta.discourse.org/t/removal-of-ember-string-prototype-extensions/283513/5 "2023-10-26T10:12:52Z")

</div>

谢谢 David。我同意这个策略。您提供 PR 很贴心。 🙏
