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 badges2 Answers
Reset to default 3If 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.
本文标签:
版权声明:本文标题:javascript - What is the best way to export library method, when using Closure Compiler? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744763684a2623903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论