我正在使用gulp爲我的項目構建資產管道。我目前使用gulp-uglify
來縮小javascript。Minifying javascript使函數不可調用
當Uglify處理我的JS時,它會更改函數名稱,使其不可調用。有誰知道如何防止這種情況發生?
(function() { "use strict";
function Ajax(params, url, callbackFunction) {
this.params = params,
this.url = url,
this.callback = callbackFunction
}
Ajax.prototype.call = function() {
$.ajax({
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "post",
data: this.params,
success: this.callback,
error: this.callback
});
}
})();
成爲
!function(){"use strict";function t(t,a,s){this.params=t,this.url=a,this.callback=s}t.prototype.call=function(){$.ajax({contentType:"application/json; charset=utf-8",dataType:"json",type:"post",data:this.params,success:this.callback,error:this.callback})}}();
這改變了Ajax的功能,被稱爲「T」而當我想以實例化一個新的Ajax對象,我得到一個Uncaught ReferenceError: Ajax is not defined
我的調用代碼是
$(document).ready(function() {
var ajaxTest = new Ajax("google.com", {}, printTest);
console.log(ajaxTest);
Ajax.call();
function printTest() {
console.log("Hello");
}
});
它被縮小到
$(document).ready(function(){function o(){console.log("Hello")}var l=new Ajax("google.com",{},o);console.log(l),Ajax.call()});
您的代碼在縮小前後都會失敗。在這兩種情況下,「Ajax」都不是全球性的,所以它不應該在準備好的文檔中被訪問。 –