Javascript Tutorial: how to optimize if short statements

Source: Internet
Author: User

UglifyJS is a tool for compressing and beautifying javascript. In its documentation, I see several methods for optimizing if statements. Although I haven't used it for some trial tests, I can see from here that it has done some beautification work on js. Some people may think that the if statement is so simple and can be optimized to what extent? But if you look at the following methods, you may change your mind.

I. Use common ternary Operators

If (foo) bar (); else baz () ;=> foo? Bar (): baz ();
If (! Foo) bar (); else baz () ;=> foo? Baz (): bar ();
If (foo) return bar (); else return baz () ;=> return foo? Bar (): baz ();

You are certainly not familiar with the above use of the ternary operator to optimize the if statement. Maybe you often use it.

Example provided by the home of scripts:

Copy codeThe Code is as follows:
<Script>
Var I = 9
Var ii = (I> 8 )? 100:9;
Alert (ii );
</Script>

Output result:

100

2. Use the and (&) and or (|) Operators

If (foo) bar () ;=> foo & bar ();
If (! Foo) bar () ;=> foo | bar ();

Honestly, I didn't write code like this. I saw it when I learned "laruence's Linux house dish", but I didn't expect to implement it in js.

3. Omit braces {}

If (foo) return bar (); else something () ;=>{ if (foo) return bar (); something ()}

You are familiar with this writing method, but I suggest you do it during code optimization or hand it over to UglifyJS to help you solve it. After all, there is one less braces, and the code is not highly readable.

Here, I think of a method for getting HTML element attributes in "proficient JavaScript" by the father of jQuery.

Function getAttr (el, attrName ){
Var attr = {'for': 'htmlfor ', 'class': 'classname'} [attrName] | attrName;
};

If we do not write this way, we may need to use two if statements for processing. The above code is not only concise and effective, but also highly readable.

Think about it. In some cases, we can find an effective way to solve the problem, but the key is whether we try to find a better way.

[Javascript tips] short for if (x = null)

If (x = null) or if (typeof (x) = 'undefined') can be abbreviated as if (! X), not verified.

Otherwise, if (x) indicates that x is not null.

Determine whether an object exists

Copy codeThe Code is as follows:
If (document. form1.softura9 ){
// Determine whether softur95. js errors are prevented.
}

Copy codeThe Code is as follows:
If (document. getElementById ("softura9 ")){
// Determine whether softur95. js errors are prevented.
}


Supplement:

Javascript | & abbreviated as if

Copy codeThe Code is as follows:
<Script type = "text/javascript">

If you want to write
If (! False)
{
Alert ('false ');
}

Consider writing it as follows:
False | alert ('false ');

False | alert ('false'); true | alert ('true'); // output false;
If "|" is used, the first condition is true. If the second condition is not detected, true is returned directly. If the first condition is false, the second condition is detected.

False & alert ('false'); true & alert ('true'); // output true
When "&" is used, the first condition is true, and the second condition is detected. If the first condition is false, the system returns false to exit.

In short, it is simple and practical to replace if ,? : Replace the if else utility. Write short and concise code

Usage:
$ ("# Regform input [type! = Hidden] "). each (
Function (index ){
$ (This ). parent (). has ("div. valid-under "). length | $ ('<div class = "valid-under"> </div> '). appendTo ($ (this ). parent ());
}
);
</Script>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.