Prevent uglifyjs from renaming certain functions

馋奶兔 提交于 2019-12-02 00:53:48

问题


I have a function that has a constructor within it. It creates a new object and returns it:

function car() {
   function Car() {}
   return new Car();
}

As a result uglify renames Car to some letter and when this returns it looks like the object name is just some letter. In chrome for instance it will say the type of the object is "t".

Is there a way to tell uglify to preserve some function's name?


回答1:


You need to use the reserved-names parameter:

--reserved-names “Car”



回答2:


Even if you follow Bill's suggestion, there's still a problem with your approach.

car().constructor !== car().constructor

One would expect those to be equal

I would change your approach to creating a constructor and giving it a Factory constructor

/** @private */
function Car() {
   ...
}

Car.create = function() {
    return new Car();
}

Or the following (module pattern), combined with Bill's approach. Then you're not returning an object with a different prototype every time

var car  = (function() {
   function Car() {...}
   return function() {
       return new Car();
   }
})();

// car().constructor === car().constructor // true


来源:https://stackoverflow.com/questions/12807263/prevent-uglifyjs-from-renaming-certain-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!