使用 Closure Compiler 时导出库方法的最佳方法是什么?

发布于 2025-01-01 23:01:07 字数 879 浏览 0 评论 0原文

Closure Compiler 文档明确指出:“不要使用 Externs 而不是 Exports”。因为 Extern 使用起来非常方便,所以我遇到了一个问题:

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

当将 Closure Compiler 与 ADVANCED_OPTIMIZATIONS 一起使用时,函数调用会从源代码中删除。可以通过两种方式来防止这种情况: 在原型定义之后添加这一行:

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

但这在输出代码的末尾添加了丑陋的代码和平:

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

我设法通过将这一行添加到构造函数来摆脱这一点:

this.invoke = this.invoke;

并将这一行添加到 externs 文件:

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

这样,闭包编译器无法从输出代码中删除invoke函数,因为它是在构造函数中自行分配的,而且它无法重命名它,因为它是在externs文件中定义的。 那么巫婆的方法更好吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滥情哥ㄟ 2025-01-08 23:01:07

如果您始终使用 JSDoc,则可以使用 @export 标签

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

并使用 --generate_exports 标志调用编译器。

这要求您包含 base.js< /a> 从 Google Closure 库,或将 goog.exportSymbolgoog.exportProperty 复制到您的代码库。

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.

雨的味道风的声音 2025-01-08 23:01:07

就我个人而言,我喜欢在 externs 文件中定义接口并让我的内部类实现它们。

// 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
  }  
}

您仍然导出构造函数本身,但不导出接口方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文