Home > Web Front-end > JS Tutorial > body text

Interesting UglifyJS

php中世界最好的语言
Release: 2018-03-16 10:53:13
Original
2048 people have browsed it

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.

1. Immediately execute the function

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.

2.Variable nameReplacement

This is natural. Function names, parameter names, and variable names are all replaced with single letters. Even code in the form of ‘_’

3. Function on top

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;
Copy after login

will be replaced by:

function a(){}function b(){}var s={}return  s.a={},s.b={},s
Copy after login

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);
  }
Copy after login

Execute xx() and get 2. If there is a function that does not return a value after rt(2), then xx() will get undefined.

4.bool value replacement

false-->!1  true-->!0
Copy after login

5.if

The if statement is the most compressed place.

1) Return prefix:

 function load() {            if (t) {
                x = false;
                log("error");                return;
            }
            console.log("22")
        }
Copy after login

For example, my original function looks like this. After compression, it looks like this:

  if (t) return x =!1,void log("error")
Copy after login

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");
  }
Copy after login

After compression:

    function f() {
            x || console.log("doA"), console.log("doB");
      }
Copy after login

This is pretty good. Similarly:

(x&&y){
 doa();
 dob(); 
}
 doc();  
 -->    x&&y&&(doa(),dob()),doc()
Copy after login

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");
      }
Copy after login

Merged like this:

 if (console.log("doA"), console.log("doB"), x > 0) console.log("true");
Copy after login

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);
  }
Copy after login

After compression:

 if (a)  throw x.triggerCallback(o, "模型验证错误"), Error(a)
Copy after login

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.

6. Number processing

Hundreds and thousands will be processed into scientific notation 1000 -->1e3.

7. while

var offset = 0;            while (true) {                if (offset >= bufferLength) {                    break;
                }
}
Copy after login

will be replaced with this:

  for (var n = 0; ; ) {                if (n >= K)                    break
 }
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!