缩小 javascript 使函数不可调用
Minifying javascript makes function un-callable
我正在使用 gulp 为我的项目构建资产管道。我目前正在使用 gulp-uglify
缩小 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
标识符在范围内是本地的,因此在范围之外无论如何都无法访问它。该范围内任何使用 Ajax
标识符的代码都将被缩小为使用 t
标识符。
如果你想在范围外使用 Ajax
标识符,你可以 return 从函数包装器中使用它:
var Ajax = (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
});
};
return Ajax;
})();
现在您可以在其他代码中使用 Ajax
标识符。如果通过缩小代码更改范围外的 Ajax
标识符,则使用它的代码也将使用该新标识符。
我正在使用 gulp 为我的项目构建资产管道。我目前正在使用 gulp-uglify
缩小 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
标识符在范围内是本地的,因此在范围之外无论如何都无法访问它。该范围内任何使用 Ajax
标识符的代码都将被缩小为使用 t
标识符。
如果你想在范围外使用 Ajax
标识符,你可以 return 从函数包装器中使用它:
var Ajax = (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
});
};
return Ajax;
})();
现在您可以在其他代码中使用 Ajax
标识符。如果通过缩小代码更改范围外的 Ajax
标识符,则使用它的代码也将使用该新标识符。