如何在ADVANCED_OPTIMIZATIONS的闭包编译器中导出公共类方法

How to export public class methods in Closure Compiler in ADVANCED_OPTIMIZATIONS?

本文关键字:类方法 编译器 闭包 ADVANCED OPTIMIZATIONS      更新时间:2023-09-26

我正在使用受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();
执行此操作

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

看:

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

及其"导出"方法。