繁体   English   中英

Google Closure编译器中的高级编译

[英]Advanced Compilation in google closure compiler

我有这段代码,我正在尝试使用闭包编译器来优化代码。

var HrevertGraph = HrevertGraph || {};
HrevertGraph.CircularProgress = (function(){
  function Progress(){
    //Properties
  }

  Progress.prototype.getCenter = function() {
   //method
  }

  //and other methods
  return Progress;

})();
window["HrevertGraph.CircularProgress"] = HrevertGraph.CircularProgress;

我在高级选项模式下进行了编译,问题是编译后的代码未显示构造函数Progress和其原型方法。 在这种情况下我该怎么办?

如果您不希望闭包编译器更改原型成员,则可以执行以下操作:

Progress.prototype['getCenter']=function....

但是,您还必须在调用此函数的已编译代码中使用括号符号。

导出构造函数时,您可以执行以下操作:

window["HrevertGraph"] = {};
window["HrevertGraph"]["CircularProgress"] = HrevertGraph.CircularProgress;

[UPDATE]

您可以在运行未编译代码的页面控制台中运行以下功能。 控制台中的输出是导出,复制并粘贴到要编译的代码中,它将导出构造函数和原型。 例如,特定的成员(使用this.something = ...在Progress中定义的成员)没有简单的脚本来生成导出。 最好在Progress构造函数中使用this['something']=this.somethingthis['something']=this.something来处理这些问题。

function exportConstructor(objPath){
  var i = -1,len = objPath.length,ret=[]
  ,exPath='window',clPath='window',
          o,thing;
  while(++i<len){
    exPath=exPath+'["'+objPath[i]+'"]';
    clPath=clPath+'.'+objPath[i];
    ret.push(exPath);
    ret.push("=");
    ret.push(clPath);
    ret.push(";\n");
  }
  var i = 0,o=window[objPath[i]];
  while(++i<len){
    o=o[objPath[i]];
  }
  o=o.prototype;
  exPath=exPath+'["prototype"]';
  clPath=clPath+'.prototype';
  for(thing in o){
      if(Object.hasOwnProperty.call(o,thing)){
      exPath=exPath+'["'+thing+'"]';
      clPath=clPath+'.'+thing;
      ret.push(exPath);
      ret.push("=");
      ret.push(clPath);
      ret.push(";\n");
    }
  }
  return ret.join("");
}
console.log(exportConstructor(['HrevertGraph','CircularProgress']));

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM