![](/img/trans.png)
[英]How to have an array of classes with static methods when using the closure compiler?
[英]How to force Google Closure Compiler to rename methods even when using objects
似乎當我使用“ this”時,谷歌關閉編譯器( http://closure-compiler.appspot.com/home )保留了所有方法名稱。 或使用顯示模塊模式時。 僅當方法不在其他外部方法可能引用的對象中時,才將方法重命名為字母。 我嘗試包裝我的(實用程序)對象和使用它的匿名方法,但這似乎無濟於事。 如何創建可能彼此共享並且仍然使所有方法名稱都縮寫的對象(和單獨的腳本文件)?
我已經閱讀了幾則關於SO的帖子,但看不到任何解決此問題的信息,只是關於與外部變量沖突而導致無法重命名的帖子。
var Utility = (function () {
this.isDefined = function (v) {
/// <summary>Checks to see if a variable is defined.</summary>
return typeof v !== 'undefined';
}
this.isObj = function (v) {
/// <summary>Checks to see if a variable is an object.</summary>
return typeof v === 'object';
}
...
})();
然后,我希望能夠做到:
(function(u) {
u.isDefined();
})(Utility);
我也嘗試過:
var Utility = (function () {
var utility = {};
utility.isDefined = function (v) {
/// <summary>Checks to see if a variable is defined.</summary>
return typeof v !== 'undefined';
}
utility.isObj = function (v) {
/// <summary>Checks to see if a variable is an object.</summary>
return typeof v === 'object';
}
return utility;
...
揭示對象模式不適用於閉包編譯器。 您正在使用的額外的閉包也不會別名窗口和文檔全局變量。 最終用例本身對於Closure編譯器也是有問題的。 當前,編譯器不能很好地處理別名,尤其是在將命名空間作為函數參數傳遞時。
// ==ClosureCompiler==
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==
var Utility = {};
Utility.isDefined = function (v) {
/// <summary>Checks to see if a variable is defined.</summary>
return typeof v !== 'undefined';
};
Utility.isObj = function (v) {
/// <summary>Checks to see if a variable is an object.</summary>
return typeof v === 'object';
};
(function() {
function myPrivateFunction(x) {
}
Utility.SomeMethod = function(x) {
return myPrivateFunction(x);
};
})();
Utility.isDefined(x);
window['Utility'] = Utility;
可以在http://closure-compiler.appspot.com/上測試此示例的編譯和重命名
通過使用output_wrapper
標志並正確導出符號,可以防止局部變量滲入全局范圍。
編譯器還特別禁止在非構造函數,非原型函數中使用this
關鍵字。 您應該始終使用完整的名稱空間。 然后,編譯器可以展平名稱空間方法(這就是限制發生的原因)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.