我如何将函数标记为“私有”通过 Google Closure Compiler 重命名它?

发布于 2024-08-18 14:03:02 字数 336 浏览 3 评论 0原文

我有 private function createSomething():

function Player(id) {

  /**
   *  Creates stuff
   *  @private
   */
  this.createSomething = function() {
    // do something good
  };
}

并且我想在使用 Google Closure Compiler 编译源代码后看到重命名的函数“createSomething()”。 是的,我知道 ADVANCED_OPTIMIZATIONS 但它与 jQuery 和其他库不兼容。

I have private function createSomething():

function Player(id) {

  /**
   *  Creates stuff
   *  @private
   */
  this.createSomething = function() {
    // do something good
  };
}

and I want to see the renamed function "createSomething()" after compiling the source with Google Closure Compiler.
Yes, I know about ADVANCED_OPTIMIZATIONS but it is incompatible with jQuery and other libraries.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相对绾红妆 2024-08-25 14:03:02

解决方案是使用字符串文字来引用该属性。

function Player(id) {
  /**
   *  @private
   */
  this['createSomething'] = function() {
    // do something good
  };
}

这是可行的,因为编译器从不重命名字符串文字。 但要小心。

您可以使用 ADVANCED_OPTIMIZATIONS 编译代码并且仍然与其他库兼容。您需要阅读库文档中有关externsexports的内容:

The solution is to use a string literal to refer to the property.

function Player(id) {
  /**
   *  @private
   */
  this['createSomething'] = function() {
    // do something good
  };
}

This works because the compiler never renames string literals. But be careful.

You can compile your code with ADVANCED_OPTIMIZATIONS and still have you compatibility with other libraries. You'll need to read about externs and exports in the library documentation:

零度℉ 2024-08-25 14:03:02

只需使用没有这个

function Player(id) {

  /**
   *  Creates stuff
   *  @private
   */
  createSomething = function() {
    // do something good
  };
}

Just use without this

function Player(id) {

  /**
   *  Creates stuff
   *  @private
   */
  createSomething = function() {
    // do something good
  };
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文