闭包编译器导出所有原型&静态方法
有没有一种简单的方法可以让闭包编译器导出一个类及其所有原型和原型?静态方法并将名称保留为公共 API?默认情况下,高级选项会重命名所有变量,但您可以将内容导出到全局范围,例如:
window['MyClass'] = MyClass;
但是,这只将 MyClass 导出到全局范围,所有原型和静态方法都被重命名。人们可能会认为您可以循环遍历原型并将其导出,但不:
for (var i in MyClass.prototype) {
window['MyClass'].prototype[i] = MyClass.prototype[i];
}
这是行不通的。我知道的唯一方法是像这样手动添加它们:
window['MyClass'].prototype['myFunction'] = MyClass.prototype.myFunction;
我想公开大约 50 个原型,所以这种方法不是首选。有谁知道如何方便地导出整个班级?
Is there an easy way for the closure compiler to be able to export a class and all it's prototypes & static methods and keep the names as a public API? Per default, the advanced option renames all variables, but you can export stuff to the global scope like:
window['MyClass'] = MyClass;
However, this only exports MyClass to the global scope, all prototypes and static methods are renamed. One would think that you can loop through through the prototypes and export them, bu no:
for (var i in MyClass.prototype) {
window['MyClass'].prototype[i] = MyClass.prototype[i];
}
This doesnt work. The only way I know is to manually add them like this:
window['MyClass'].prototype['myFunction'] = MyClass.prototype.myFunction;
I want to expose about 50 prototypes so this method is not preferred. Does anyone know how to export the entire class in a handy way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所描述的实际上是 extern 的用途:
阻止 Google Closure Compiler 重命名设置对象
您可以在此处查看大型 externs 文件的示例:
http://code。 google.com/p/closure-compiler/source/browse/trunk/contrib/externs/jquery-1.6.js
您可以省略所有注释,只使用如下语句:
确保 add 方法不是重命名。您确实需要在 externs 文件的注释中包含 @externs 或将其作为 --externs 传递给闭包编译器才能正常工作。
What you describe is really what externs is for:
Prevent Google Closure Compiler from renaming settings objects
You can see an example of a large externs file here:
http://code.google.com/p/closure-compiler/source/browse/trunk/contrib/externs/jquery-1.6.js
You can leave out all the comments and just use statements like:
To ensure the add method isn't renamed. You do need to either include @externs in the comments of the externs file or pass it as --externs to the Closure Compiler for things to work properly.
查看 JavaScript 中记录的 @export 注释
风格指南:http://google-styleguide.googlecode。 com/svn/trunk/javascriptguide.xml?showone=Comments#Comments
Check out the @export annotation, which is documented in the JavaScript
style guide: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Comments#Comments