As for judgment, it means choosing when facing 2 or more options. For example, there is a fork in front of my house when I go to the company. As long as I know which road is right for the first time, I don’t have to think about it next time or every time in the future. Just take that road - of course, it doesn’t count when a sudden natural disaster occurs.
It takes time to make a judgment and the corresponding conditions are required. The correct judgment is good, but every time I face a fork in the road, even if I have walked countless times, I will make a judgment. It is undoubtedly a kind of brainless behavior.
Let’s take a look at a JS function that we often see. Its function is to judge the browser type and set the corresponding transparency attribute:
function setAlpha(obj,alpha){
if (-[1,]) = alpha / 100;
else = "alpha(opacity=" + alpha + ")";
}
Is this function wrong? No. First, determine whether the browser is a standard browser. If so, set transparency directly through opacity; if not, use IE filters to achieve transparency.
Clear logic, concise code, and exquisite quality!
However, there are no mistakes, but there are mistakes.
Usually, this kind of transparency function is used to create a fade effect, that is, it will be called again and again by setTimeout until the end of the loop.
The problem arises at this time. Going back to the previous fork-off road problem, this function is equivalent to thinking and judgment every time you come to an intersection. The first time, I came to the intersection, and I took the first road, and the second time, I came to the intersection, and the second time, I came to the intersection, and the second time, I took the first road, and the third time, I took the fourth road. . . . No matter how many times, this function will check your identity like the most dedicated traffic policeman———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— If you don’t get upset, I’m annoyed!
At this time, if you are passing by, you will definitely hope that the traffic police will disappear.
In fact, when we encounter a fork in the road, we can do this: Since we already know that we only walk one road, then I might as well block the other road! Of course, this is impossible in real life, but it is not difficult to implement in code, just change the idea.
There is a magical thing about javascript, which is anonymous functions (usually from execution). The self-executing function means that it is executed when it declares, and it will not appear again in the future. You may not be able to find it if you want to find it! Personally, this is a good sex.
Look at the following code, which is also a function that sets transparency:
var setAlpha = (function(obj,alpha){
var set;
if (-[1,]) {
set = function (obj,alpha) {
= alpha * 0.01;
}
}
else {
set = function(obj,alpha){
= "alpha(opacity=" + alpha + ")";
}
}
return set;
})()
Maybe you are dissatisfied: What a thing this is, it seems like the level of a beginner (you have seen through it...) But! This one is much more efficient than the previous version. If you don't believe it, you can alert this function separately under firefox and ie6, and you will understand.
By self-executing the function, the function is executed when the setAlpha is declared. The function's function is to judge the browser and determine which method to set transparency should be used. Since the browser type cannot be changed after opening the page, it means there is no need to judge again in the future. Even if you call this function 100,000 times, he will not make any judgments, but will execute them directly.
Although the code is ugly, the realm is different...
This example is just a small demonstration. I just hope you can understand the principle of [judgment once, not judge again and again] and carry it forward. Reducing the number of judgments is a great improvement in the efficiency of JS.
Reprinted from:
It takes time to make a judgment and the corresponding conditions are required. The correct judgment is good, but every time I face a fork in the road, even if I have walked countless times, I will make a judgment. It is undoubtedly a kind of brainless behavior.
Let’s take a look at a JS function that we often see. Its function is to judge the browser type and set the corresponding transparency attribute:
Copy the codeThe code is as follows:
function setAlpha(obj,alpha){
if (-[1,]) = alpha / 100;
else = "alpha(opacity=" + alpha + ")";
}
Is this function wrong? No. First, determine whether the browser is a standard browser. If so, set transparency directly through opacity; if not, use IE filters to achieve transparency.
Clear logic, concise code, and exquisite quality!
However, there are no mistakes, but there are mistakes.
Usually, this kind of transparency function is used to create a fade effect, that is, it will be called again and again by setTimeout until the end of the loop.
The problem arises at this time. Going back to the previous fork-off road problem, this function is equivalent to thinking and judgment every time you come to an intersection. The first time, I came to the intersection, and I took the first road, and the second time, I came to the intersection, and the second time, I came to the intersection, and the second time, I took the first road, and the third time, I took the fourth road. . . . No matter how many times, this function will check your identity like the most dedicated traffic policeman———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— If you don’t get upset, I’m annoyed!
At this time, if you are passing by, you will definitely hope that the traffic police will disappear.
In fact, when we encounter a fork in the road, we can do this: Since we already know that we only walk one road, then I might as well block the other road! Of course, this is impossible in real life, but it is not difficult to implement in code, just change the idea.
There is a magical thing about javascript, which is anonymous functions (usually from execution). The self-executing function means that it is executed when it declares, and it will not appear again in the future. You may not be able to find it if you want to find it! Personally, this is a good sex.
Look at the following code, which is also a function that sets transparency:
Copy the codeThe code is as follows:
var setAlpha = (function(obj,alpha){
var set;
if (-[1,]) {
set = function (obj,alpha) {
= alpha * 0.01;
}
}
else {
set = function(obj,alpha){
= "alpha(opacity=" + alpha + ")";
}
}
return set;
})()
Maybe you are dissatisfied: What a thing this is, it seems like the level of a beginner (you have seen through it...) But! This one is much more efficient than the previous version. If you don't believe it, you can alert this function separately under firefox and ie6, and you will understand.
By self-executing the function, the function is executed when the setAlpha is declared. The function's function is to judge the browser and determine which method to set transparency should be used. Since the browser type cannot be changed after opening the page, it means there is no need to judge again in the future. Even if you call this function 100,000 times, he will not make any judgments, but will execute them directly.
Although the code is ugly, the realm is different...
This example is just a small demonstration. I just hope you can understand the principle of [judgment once, not judge again and again] and carry it forward. Reducing the number of judgments is a great improvement in the efficiency of JS.
Reprinted from: