简体   繁体   English

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

[英]How i can mark function as “private” to renaming it by Google Closure Compiler?

I have private function createSomething(): 我有私有函数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. 并且我想在使用Google Closure Compiler编译源代码后看到重命名的函数“ createSomething()”。 Yes, I know about ADVANCED_OPTIMIZATIONS but it is incompatible with jQuery and other libraries. 是的,我知道ADVANCED_OPTIMIZATIONS,但它与jQuery和其他库不兼容。

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. 您可以使用ADVANCED_OPTIMIZATIONS编译代码,但仍与其他库兼容。 You'll need to read about externs and exports in the library documentation: 你需要了解的库文件中实习医生出口

Just use without this 不用这个就可以使用

function Player(id) {

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能告诉Google闭包编译器不要删除var - How can I tell Google closure compiler to not remove a var 如何在JavaScript中为Google闭包编译器设置参数类型? - How can I set type of arguments in JavaScript for Google closure compiler? 如何使用^:export标记用`reify`创建的方法,以便Closure编译器不重命名它们? - How can I mark methods created with `reify` with ^:export, so that the Closure compiler doesn't rename them? 我可以告诉Closure编译器,仅针对特定类型,停止重命名属性吗? - Can I tell the Closure compiler to, for specific types only, stop renaming properties? 防止Google Closure Compiler重命名设置对象 - Prevent Google Closure Compiler from renaming settings objects 我可以使用 Google 闭包编译器编译 SJCL 库吗? - Can I compile SJCL library with Google closure compiler? 如何使用 Google Closure Compiler 减小 Angularjs 站点的大小? - How can I reduce the size of an Angularjs site using Google Closure Compiler? 如何帮助Google的Closure Compiler优化我的JavaScript代码? - How can I help Google's Closure Compiler optimize my JavaScript code? 如何在Google Closure Compiler中启用标头字符串文字声明? - How can I enable header string literal declarations in Google Closure Compiler? 如何使用Google Closure编译器 - How to use Google Closure compiler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM