简体   繁体   English

缩小javascript会使函数无法调用

[英]Minifying javascript makes function un-callable

I'm using gulp to build an asset pipeline for my project. 我正在使用gulp为我的项目建立资产管道。 I'm currently using gulp-uglify to minify javascript. 我目前正在使用gulp-uglify来缩小javascript。

When Uglify processes my JS, it changes the function name, which makes it un-callable. 当Uglify处理我的JS时,它会更改函数名称,从而使其无法调用。 Does anyone know how to prevent this from happening? 有谁知道如何防止这种情况的发生?

(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
    });
  }
})();

becomes 变成

!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})}}();

This changes the Ajax function to be called "t" and when I want to instantiate a new Ajax object, I get an Uncaught ReferenceError: Ajax is not defined 这将Ajax函数更改为“ t”,并且当我想实例化一个新的Ajax对象时,我收到了Uncaught ReferenceError: Ajax is not defined

My calling code is 我的通话代码是

$(document).ready(function() {
  var ajaxTest = new Ajax("google.com", {}, printTest);
  console.log(ajaxTest);
  Ajax.call();

  function printTest() {
    console.log("Hello");
  }
});

which gets minified to 缩小到

$(document).ready(function(){function o(){console.log("Hello")}var l=new Ajax("google.com",{},o);console.log(l),Ajax.call()});

There is nothing wrong with the minifying process. 缩小过程没有错。 The Ajax identifier is local to the scope, so it's not reachable outside the scope anyway. Ajax标识符在作用域内是本地的,因此无论如何都无法在作用域外访问。 Any code inside that scope that would use the Ajax identifier would be minified to use the t identifier. 该范围内将使用Ajax标识符的任何代码都将被压缩以使用t标识符。

If you want to use the Ajax identifier outside the scope, you can return it from the function wrapper: 如果要在范围之外使用Ajax标识符,可以从函数包装器返回它:

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;
})();

Now you can use the Ajax identifier in your other code. 现在,您可以在其他代码中使用Ajax标识符。 If the Ajax identifier that is outside the scope is changed by minifying the code, the code using it will also use that new identifier. 如果通过缩小代码来更改范围之外的Ajax标识符,则使用该标识符的代码也将使用该新标识符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM