Minify and variable names

核能气质少年 提交于 2019-12-10 14:34:50

问题


I know that minification is responsible for removing: white space characters, new line characters, comments, and sometimes block delimiters. Not long ago I read it's also responsible for shorten variable names. But I always thought it's a part of obfuscation. Am I right? Or now minification libraries also include such functionality?


回答1:


Well, since the objective of minification is to reduce the size of the code as much as possible, renaming variables is an effective way of doing just that.

A trick that JavaScript minifiers often use, is to wrap the code in a immediately executed function, with a lot of arguments:

(function(a,b,c,d,e,f,g){/* ... */})();

This makes it possible to use these variables without declaring them with the var keyword, thus reducing the size of your code by three bytes times the number of var keywords.

Modern minifiers use a lot of these advanced tricks to reduce the size of your code, that the code seems obfuscated is just a bi-product of the minification.



来源:https://stackoverflow.com/questions/12768840/minify-and-variable-names

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