Improve JavaScript efficiency one time judgment without a second judgment _javascript skill

Source: Internet
Author: User
The judgment is to choose when faced with 2 or more options. For example, my door to the company has a fork in the road, as long as I know which way is right, then I do not have to think about the next time or later, go that way directly--of course, sudden natural disasters do not count.
It takes time to make a judgment, and it needs a corresponding condition. The correct judgment is very good, but each face fork, even if is to walk countless times fork, all come to judge, is undoubtedly a kind of brain residual behavior.

Here are a few of the JS functions we often see, his role is to determine the browser type and then set the corresponding transparency properties:
Copy Code code as follows:

function Setalpha (obj,alpha) {
if (-[1,]) obj.style.opacity = alpha/100;
else Obj.style.filter = "Alpha (opacity=" + alpha + ")";
}

Is this function wrong? No. First determine whether the browser is a standard browser, if it is, directly through the opacity set transparent, if not, use the IE filter to achieve transparency.

Logic clear, Code concise, boutique Ah!

However, there is no mistake, but there are mistakes.

Typically, this set of transparency functions is used to make a fade effect, which means it will be settimeout and called again and again until the loop ends.

That's when the problem comes. Back to the fork in front of the problem, this function is equivalent, every time you come to the intersection, you have to think about judgment. The 1th time, came to the intersection, Sao and so on, I look, oh, is Firefox ah, take the 1th Road, 2nd time, and came to this intersection, and so on, I look, oh, is Firefox ah, also go 1th road ... The 3rd time ... The 4th time ..... No matter how many times, this function will be like the most dedicated traffic police to see your identity ———— you, do not bother? I'm sick of you bothering me!

This time, if you are passing by, you must hope that the traffic policeman disappears.

In fact, we can do this: since already know to go only one way, then I simply put another way to stop the dead forget! Of course in the real life can not do so, but in the code is not difficult to achieve, change the train of thought can be.

JavaScript has a magical place, is the anonymous function (usually from the execution), since the execution of the function of the meaning is, he declared the execution of the drop, will not appear again, you want to find can not find! Personally, I think it's a good sex.

Look at the following code, which is also a function of setting transparency:

Copy Code code as follows:

var Setalpha = (function (obj,alpha) {
var set;
if (-[1,]) {
set = function (Obj,alpha) {
obj.style.opacity = Alpha * 0.01;
}
}
else {
set = function (Obj,alpha) {
Obj.style.filter = "Alpha (opacity=" + alpha + ")";
}
}
return set;
})()

Perhaps you are dissatisfied: what is this thing ah, like the level of beginners (you see through ...) But This is much more efficient than the previous version. Do not believe you can in Firefox and IE6 under the separate alert this function, you understand.

By executing the function, the function is executed when the declaration is setalpha, and the function is to judge the browser and determine which method of setting the transparency to use. Because the browser type in the open page will not be able to change, that is, no longer need to judge. Even if you call this function 100,000 times, he will not judge it, but execute it directly.

Although the code is ugly, but the state is different ...

This example is just a small demonstration, I just hope you can understand the "one judgment, but not the second judgment" of the truth, and carry forward. Reduce the number of judgments, the efficiency of JS is a great improvement.
Turn from: jo2.org
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.