This time I bring you interesting UglifyJS. What are the precautions for using UglifyJS? Here are practical cases, let’s take a look.
It’s not like I have nothing to do to look at the compression code, but I found it interesting when debugging my own code today. Because I wrote it myself, even though it is compressed, it is still easy to read after formatting. Of course, the first criterion as a min is not readability, but simplicity. Then it will try to shorten the code as much as possible, keep it on one line as much as possible, and minimize the white space. The semicolons we commonly use will be replaced by commas, and short sentences will become long and coherent sentences.
I originally wrote it in the second way, but uglify replaced it with the first way ( Of course shorter). In fact, the function of brackets and ! symbols is to convert the function part into a expression, rather than a declaration. In this way, it can be executed immediately, and similarly ~ + can be done.
This is natural. Function names, parameter names, and variable names are all replaced with single letters. Even code in the form of ‘_’
function foo (){} will be placed at the top of the module. Of course, this is a norm, but later I found that it also has another function, which is to facilitate subsequent code merging. For example, if we define it like this:
var self=this;function a(){} self.a=a;function b(){} self.b=b;return self;
will be replaced by:
function a(){}function b(){}var s={}return s.a={},s.b={},s
Note that the last s cannot be missed, and the return will be based on the result of the last expression.
function rt(n) { return n; } function xx() { return rt(1), rt(2); }
Execute xx() and get 2. If there is a function that does not return a value after rt(2), then xx() will get undefined.
false-->!1 true-->!0
The if statement is the most compressed place.
1) Return prefix:
function load() { if (t) { x = false; log("error"); return; } console.log("22") }
For example, my original function looks like this. After compression, it looks like this:
if (t) return x =!1,void log("error")
return is advanced and there is an extra void at the end. Why is this. Without the curly braces, the four sections of if code become one sentence. The function of void here is to erase the return value of the function. Because the original if has no return value. If the log method has a return value at this time. Then calling load will get the return value. This creates interference and defeats the purpose of the original function. So I erased it with void.
2) Short circuit
function foo() { if (!x) { return; } console.log("doA"); console.log("doB"); }
After compression:
function f() { x || console.log("doA"), console.log("doB"); }
This is pretty good. Similarly:
(x&&y){ doa(); dob(); } doc(); --> x&&y&&(doa(),dob()),doc()
The original four lines became one line of code.
3). In order to merge a line, this also works:
console.log("doA"); console.log("doB"); if (x>0) { console.log("true"); }
Merged like this:
if (console.log("doA"), console.log("doB"), x > 0) console.log("true");
It may not be very friendly to write like this, but the key point is in the if statement, The last sentence is the judgment sentence. Combined with the previous return. You must have a deep understanding of comma statements.
4) Don’t let throw go either
if (errMsg) { util.triggerCallback(fail, "模型验证错误"); throw Error(errMsg); }
After compression:
if (a) throw x.triggerCallback(o, "模型验证错误"), Error(a)
Change the order of statements, and you will understand if you regard throw as return.
5) if else
This will be replaced by the ternary expression a?b:c.
Hundreds and thousands will be processed into scientific notation 1000 -->1e3.
var offset = 0; while (true) { if (offset >= bufferLength) { break; } }
will be replaced with this:
for (var n = 0; ; ) { if (n >= K) break }
is really good, saving a line of code.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to let JS automatically match proto Js
h5 makes web recording function
The above is the detailed content of Interesting UglifyJS. For more information, please follow other related articles on the PHP Chinese website!