我如何将功能标记为“私有”以通过Google Closure Compiler重命名?

时间:2010-01-14 17:00:27

标签: javascript google-closure-compiler

我有私有函数createSomething():

function Player(id) {

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

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

2 个答案:

答案 0 :(得分:3)

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

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

这是有效的,因为编译器永远不会重命名字符串文字。 But be careful.

您可以使用ADVANCED_OPTIMIZATIONS编译代码,但仍然可以兼容其他库。您需要阅读库文档中的 externs exports

答案 1 :(得分:-3)

只需在没有

的情况下使用
function Player(id) {

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