What is the best way to export library method, whe

2019-07-07 02:10发布

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?

2条回答
Rolldiameter
2楼-- · 2019-07-07 02:54

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-07 03:02

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 compiler 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.

查看更多
登录 后发表回答