admin管理员组

文章数量:1391776

Closure Compiler documentation clearly states: "Don't use Externs instead of Exports". Because Externs are very handy to use, I came down with a problem:

function Lib(){ 
  //some initialization  
}
Lib.prototype = {
  invoke : function(str){
     //this function is called from outside to invoke some of Lib's events
  }  
}

When using Closure Compiler with ADVANCED_OPTIMIZATIONS, function invoke is removed from the source. This could be prevented in two ways: Adding the line after prototype definition:

Lib.prototype['invoke'] = Lib.prototype.invoke;

But this adds an ugly peace of code at the end of the output code:

Lib.prototype.invoke = Lib.prototype.g;

I managed to get rid of this by adding this line to the constructor:

this.invoke = this.invoke;

And this line to the externs file:

/**
* @param {String} str
*/ 
Lib.prototype.invoke = function(str){};

This way, Closure Compiler can't remove invoke function from the output code, because it is assigned by itself in the constructor, and also, it can't rename it, because it is defined in the externs file. So witch method is better?

Closure Compiler documentation clearly states: "Don't use Externs instead of Exports". Because Externs are very handy to use, I came down with a problem:

function Lib(){ 
  //some initialization  
}
Lib.prototype = {
  invoke : function(str){
     //this function is called from outside to invoke some of Lib's events
  }  
}

When using Closure Compiler with ADVANCED_OPTIMIZATIONS, function invoke is removed from the source. This could be prevented in two ways: Adding the line after prototype definition:

Lib.prototype['invoke'] = Lib.prototype.invoke;

But this adds an ugly peace of code at the end of the output code:

Lib.prototype.invoke = Lib.prototype.g;

I managed to get rid of this by adding this line to the constructor:

this.invoke = this.invoke;

And this line to the externs file:

/**
* @param {String} str
*/ 
Lib.prototype.invoke = function(str){};

This way, Closure Compiler can't remove invoke function from the output code, because it is assigned by itself in the constructor, and also, it can't rename it, because it is defined in the externs file. So witch method is better?

Share Improve this question edited Apr 10, 2014 at 4:54 TM. 111k34 gold badges124 silver badges128 bronze badges asked Feb 2, 2012 at 11:57 Hitman_99Hitman_99 2,2822 gold badges19 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

If you use JSDoc consistently, you could use the @export tag:

/**
* @param {String} str
* @export
*/ 
Lib.prototype.invoke = function(str){
     //this function is called from outside to invoke some of Lib's events
};

and call the piler with the --generate_exports flag.

This requires you to either include base.js from the Google Closure library, or to copy goog.exportSymbol and goog.exportProperty to your codebase.

Personally, I like defining interfaces in externs file and having my internal classes implement them.

// Externs

/** @interface */
function IInvoke {};
IInvoke.prototype.invoke;

/** 
 *  @constructor
 *  @implements {IInvoke}
 */
function Lib(){ 
  //some initialization  
}
Lib.prototype = {
  invoke : function(str){
     //this function is called from outside to invoke some of Lib's events
  }  
}

You still export the constructor itself, but not the interface methods.

本文标签: