如何在ADVANCED_OPTIMIZATIONS中的Closure Compiler中导出公共类方法?

时间:2013-10-16 19:34:37

标签: javascript obfuscation google-closure-compiler

我正在使用受John Resig启发的JavaScript继承,我的库代码如下所示:

var Person = Class.extend({
  /** @private */
  _dancing: null,

  /** @private */
  _init: function(isDancing){
    this._dancing = isDancing;
  },

  /** @public */ 
  dance: function(){
    return this._dancing;
  }
});

var obj = new Person();
obj.dance();

只有那些以下划线开头的类方法并在ADVANCED_OPTIMIZATIONS中保存所有公共方法的最佳方法是什么。

我需要获得以下输出:

var a = Class.extend({a:null, b:function(b) {
  this.a = b;
}, dance:function() {
  return this.a;
}});
new a;
a.dance();

1 个答案:

答案 0 :(得分:0)

执行此操作的“最简单”方法是为编译器创建自定义编码约定(您必须修改编译器)并将“export”约定更改为不以“_”开头的任何内容。

见:

http://closure-compiler.googlecode.com/svn/trunk/src/com/google/javascript/jscomp/GoogleCodingConvention.java

及其“isExported”方法。