JavaScript Tutorial: A method for optimizing if shorthand statements _javascript tips

Source: Internet
Author: User

UGLIFYJS is a tool for compressing and beautifying JavaScript, and in its documentation, I've seen several ways to optimize if statements. Although I have not used it to do some experimental testing, but from here I can see that it does make JS a landscaping work. Perhaps some people think that if statement is so simple, can optimize to what extent? But if you look at the following ways, you may change your view.

First, use the common ternary operator

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 ();

For the above use ternary operator to optimize the IF statement you are certainly not unfamiliar, perhaps you often use it.

Examples given by the cloud-dwelling community:

Copy Code code as follows:

<script>
var i=9
var ii= (i>8)? 100:9;
Alert (ii);
</script>

Output results:

100

II. Use and (&&) and OR (| |) Operator

if (foo) bar (); ==> Foo&&bar ();
if (!foo) bar (); ==> foo| | Bar ();

To be honest, I didn't write code like this, I've seen it when I was studying "brother Bird's Linux private dish", but I didn't expect it to happen in JS.

Three, omit curly braces {}

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

You and I are familiar with this type of writing, but I suggest doing it in code optimization, or handing it over to Uglifyjs to help you solve it. After all, less than a curly brace, the code is not very readable.

Writing here, I think of the way in which the father of jquery is getting HTML element attributes in "proficient in JavaScript."

function GetAttr (el, Attrname) {
var attr = {' for ': ' Htmlfor ', ' class ': ' ClassName '}[attrname] | | Attrname;
};

If we do not write this, we may need to use two if statements for processing, and the above code is not only simple and effective, but also strong readability.

To think about it, we can all find an effective way to solve the problem, but the key is whether we find a better way by heart.

"JavaScript Tips" if (x==null) shorthand

if (x==null) or if (typeof (x) = = ' undefined ') can be abbreviated to if (!X) and not validated.

Conversely if (x) means X is not empty

Determine if an object exists

Copy Code code as follows:

if (DOCUMENT.FORM1.SOFTURL9) {
To determine whether there are softurl9 to prevent JS error
}

Copy Code code as follows:

if (document.getElementById ("Softurl9")) {
To determine whether there are softurl9 to prevent JS error
}


Add:

JavaScript | | && abbreviation IF

Copy Code code as follows:

<script type= "Text/javascript" >

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

It may be considered to write:
False | | Alert (' false ');

False | | Alert (' false '); true | | Alert (' true '); Output false;
With the "| |" Case, the first condition is true, and the second direct return true is not detected. The first condition is false, and the second condition detection is performed

False && alert (' false '); True && alert (' true '); Output true
In the case of "&&", the first condition is true and the second condition is detected. The first condition is false, and returns false to exit directly.

In short, is it simple and practical to replace if? : Replace the utility of if else. Write short and dapper 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.