1

I have this code and i am trying to use closure compiler to optimize the code.

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

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

  //and other methods
  return Progress;

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

I compiled this on advanced option mode and the problem is the compiled code doesn't show the constructor function Progress and its prototype methods. What should I do in this case?

1 Answer 1

2

If you don't want closure compiler to change the prototype members you can do:

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

But you have to use the bracket notation in compiled code that calls this function as well.

When you export the constructor you can do:

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

[UPDATE]

Here is a function you can run in the console of the page running your uncompiled code. The output in the console is the export, copy and paste that in your code to be compiled and it'll export the constructor and the prototype. For instance specific members (members defined in Progress with this.something=...) there is no easy script to generate export. It's best to handle that in the Progress constructor function with something like this['something']=this.something to export those.

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']));
5
  • I have many prototype methods. Do i have to change every methods to bracket notation? Or is there any way, without changing to bracket, notation i can compile it. And the method u mentioned is on the closure compiler site.
    – bring2dip
    Commented Dec 2, 2014 at 13:31
  • 1
    @DeepakBhattarai I don't think you understand the differences between the compilation levels. See closuretools.blogspot.com/2012/09/… Commented Dec 2, 2014 at 17:13
  • @DeepakBhattarai Updated the answer with some export script that can generate the code needed to export the constructor and it's prototype.
    – HMR
    Commented Dec 3, 2014 at 1:55
  • @ChadKillingsworth thankyou for the link I have now decided to use the simple compilation method. (y)
    – bring2dip
    Commented Dec 3, 2014 at 10:31
  • @HMR:thank you for the updated code. but i have decided to use the simplpe optimization. will surely use that in my projects
    – bring2dip
    Commented Dec 3, 2014 at 10:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.