# 如何在函数内部引用 \`api.addPosterIcon\`

**URL:** <https://meta.discourse.org/t/how-to-refer-to-api-addpostericon-from-inside-of-a-function/202975>\
**Category:** Development\
**Created:** [2021 年9 月 8 日 15:20 UTC](https://meta.discourse.org/t/how-to-refer-to-api-addpostericon-from-inside-of-a-function/202975 "2021-09-08T15:20:12Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

作者： ![blattersturm](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blattersturm/32/76134_2.png) [@blattersturm](https://meta.discourse.org/u/blattersturm)\
发布日期： [2021 年9 月 8 日 16:19 UTC](https://meta.discourse.org/t/how-to-refer-to-api-addpostericon-from-inside-of-a-function/202975/2 "2021-09-08T16:19:32Z")

</div>

`text/discourse-plugin` 代码片段在主题中的实现方式如下：

> <https://github.com/discourse/discourse/blob/88aa0136e372b232b89e1298d87e67ad9a4cb27f/app/models/theme_field.rb#L107-L122>

我并未直接看到此处提及 `this` 实例，这意味着它与 JavaScript 的隐式 `this` 无关；同时，由于该片段是直接粘贴进去的，也不可能是 `api` 超出作用域的问题。

这个最小示例真的能运行吗？也就是说，问题是否出在被省略的代码上？

顺便一提：这里的 `Discourse` 全局变量及其 `currentUser` 字段也可以替换为 `api.getCurrentUser()`：

> <https://github.com/discourse/discourse/blob/11a07b37e15f20f5de960243b9ad611fd477af6b/app/assets/javascripts/discourse/app/lib/plugin-api.js#L121-L122>

---

_[View the full topic](https://meta.discourse.org/t/how-to-refer-to-api-addpostericon-from-inside-of-a-function/202975)._
