Improves javascript efficiency with one judgment instead of one judgment.

Source: Internet
Author: User

To judge whether it is to select two or more options. For example, if I go to a branch road in front of my house, as long as I know which road is right for the first time, I don't have to think about it any more next time or in the future, take that path directly-of course, it is not counted in the event of natural disasters.
It takes time to make a judgment and the corresponding conditions are required. Correct judgment is good, but every time you face a wrong road, even if you have gone through countless times, it is undoubtedly a kind of braindamage.

Next, let's look at a JS function that we often see. Its role is to determine the browser type and then set the corresponding transparency attribute:
Copy codeThe Code is 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 yes, you can directly set transparency through opacity. If not, use the IE filter to achieve transparency.

Clear logic, concise code, excellent products!

However, there are no errors, but there are mistakes.

Usually, this type of transparency setting function is used to make the fade-in and fade-out effect, that is, it will be called again and again by setTimeout until the loop ends.

At this time, the problem arises. Back to the previous fork in the road, this function is equivalent to thinking and judging every time you come to the intersection. 1st times, come to the intersection, Sao, etc. Let's take a look, oh, it's firefox, take 1st steps, and 2nd times, come to the intersection again, and so on. Let's take a look, oh, it's firefox again, and it's still 1st steps ...... 3rd ...... 4th times .... No matter how many times, this function will look at your identity like the most professional traffic police-you, don't bother? I'm tired of it!

At this time, if you are on the road, you certainly want this traffic police to disappear.

In fact, we can do this when we encounter a fork in the road: since we already know that we only go one way, I will block the other way! Of course, it is impossible to do this in real life, but it is not difficult to implement it in code. Just change your mind.

The magic of javascript is that it is an anonymous function (usually used for execution). Self-executed functions mean that they are executed when they are declared, it will not appear again in the future. You may not find it! I personally think this is good.

The following code also shows the function for setting transparency:

Copy codeThe Code is 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;
})()

Maybe you are not satisfied: What is this? It seems like a beginner's level (you have seen it through ...) But! This is much more efficient than the previous version. If you do not believe this function, you can use alert in firefox and ie6 to understand it.

Through the self-execution function, the function is executed when setAlpha is declared. The function is used to determine the browser and determine which method to set transparency is used. Because the browser type cannot be changed after the page is opened, that is, it does not need to be determined in the future. Even if you call this function for 0.1 million times, it will not be judged, but will be executed directly.

Although the Code is ugly, the realm is different...

This example is just a small demonstration. I just hope that you can understand the principle of [one judgment, not one judgment] and carry forward it. Reducing the number of judgments greatly improves the js efficiency.

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.