Tip 1: setTimeout.
Application Case: for example, if you want to execute a function ten times in a loop, what should you do? SetInterval and clearInterval are usually used in the past. Skill 1 is to overcome this problem.
(Function (){
Var I = 0;
Function job (){
Console. log (I ++ );
If (I <10 ){
SetTimeout (job, 1000 );
}
}
Job ();
})();
The above job function will be executed for 10 times and then automatically stopped.
Tip 2: efficient for Loop
Application Case: abandon the traditional Cycle Mode
(Function (){
Var arr = [];
For (var I = arr. length; I --;){
DoStuff ();
}
})();
Why is this method efficient?
1. A parameter l = arr. length is missing;
2: The for statement is less than one calculation. In the past, for (I = 0; I <l; I ++) in this case, the intermediate statement will first compare I <l and then compare the result in
Compared with true or false, it is naturally calculated multiple times.
Tip 3: efficient assignment
Application Case: discard the traditional if judgment assignment
Var I = 1, ret;
Ret = I! = 1 | true;
Console. log (ret );
The above code will tell you that ret will be true. Efficient, don't use if (I! = 1) the value is assigned.
Tip 4: Powerful and short attr
Application Case: setAttribute and getAttribute. This method can not only set standard attributes, but also set any attributes, which is compatible
Function attr (elem, name, value ){
Var ret;
If (value ){
If (/msie [6-7] \. 0/I. test (navigator. userAgent )){
Ret = elem. getAttributeNode (name );
If (! Ret) {// The Property of ie6 7 is invalid. You can set it here.
Ret = document. createAttribute (name );
Elem. setAttributeNode (ret );
}
Ret. nodeValue = value + "";
} Else {
Elem. setAttribute (name, value );
}
Return elem;
} Else {// ie6 7 has a property to get
Ret = elem. getAttribute (name );
FixIe = elem. getAttributeNode (name). nodeValue;
Ret = ret? Ret: fixIe? FixIe: undefined;
Return ret;
}
}
How can I test the above methods?
Attr (document. getElementById ("test"), "classxx", "xx ")
Alert (attr (document. getElementById ("test"), "classxx "));
Tip 5: getElementsByClassName.
Application Case: When JavaScript has no framework in the past, everyone will imitate this method to see how I efficiently imitated it today. This is also a typical code for beginners of js.
(Function (){
Var getElementsByClassName = function (cls, context ){
Var root = context | document;
Return document. querySelectorAll? Root. querySelectorAll ("." + a): root. getElementsByClassName?
Root. getElementsByClassName (a): help ("*", cls, context );
}
Var help = function (tagName, cls, context ){
Var root = context | document,
Ret = [], elems, I,
Rcls = new RegExp ("^ | \ s +" + cls + "\ s + | ___ FCKpd ___ 4 quot ;);
Elems = c. getElementsByTagName (tagName | "*");
For (I = elems. length; I --;){
If (rcls. test (elem [I]. className )){
Ret. push (elems [I]);
}
}
Return ret;
}
})();
The above js lustful techniques are quite practical, provided that you do not use other people's js frameworks and use the code on the premise of native creation efficiency.
In this case, JavaScript code fans are still original at nothing. Thank you for your support. If you think you can write well, you can pin it down or send the link to your friends.
Source: blog, author blog: http://nothingbrother.cnblogs.com/