admin管理员组文章数量:1244313
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?
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?
Share Improve this question asked Oct 9, 2012 at 19:41 ParrisParris 18.4k19 gold badges96 silver badges135 bronze badges 4- 1 Why does it matter? Just for debugging purposes? You shouldn't be using the uglified version for debugging anyway. – Ruan Mendes Commented Oct 9, 2012 at 19:44
- I'd say it is more semantic than anything else. If you are exposing a library out to people they should know what type of object they are working with. – Parris Commented Oct 9, 2012 at 19:46
-
How does it help to know that the name of the constructor is
Car
? It looks like you are trying to keep it private so there should be no need for users of your library to know what the name of the constructor is. If it's a public object, there's no need to declare it within the function. – Ruan Mendes Commented Oct 9, 2012 at 19:52 - 1 I believe that there are people out there that check type based on the name of an object: stackoverflow./questions/332422/… Which may be useful in some cases? Hmmm.. perhaps not? – Parris Commented Oct 9, 2012 at 20:17
2 Answers
Reset to default 10You need to use the reserved-names
parameter:
--reserved-names “Car”
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), bined 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
本文标签: javascriptPrevent uglifyjs from renaming certain functionsStack Overflow
版权声明:本文标题:javascript - Prevent uglifyjs from renaming certain functions - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740206129a2241118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论