Record the code snippet that I have to like after seeing it. The content of this clipboard will be updated constantly. Please pay attention to the Dynamic Construction of regular expressions.
The Code is as follows:
New RegExp (Expr. match [type]. source + (/(?! [^ \ [] * \]) (?! [^ \ (] * \)/. Source ))
This avoids character escaping when a regular expression is dynamically constructed from sizzle.
More flexible and clever digital Zeros
The Code is as follows:
Function prefixInteger (num, length ){
Return (num/Math. pow (10, length). toFixed (length). substr (2 );
}
Returns the maximum and minimum values of an array.
The Code is as follows:
Math. max. apply (Math, [1, 2, 3]) // 3
Math. min. apply (Math, [1, 2, 3]) // 1
Generate beautiful random strings
The Code is as follows:
Math. random (). toString (16). substring (2); // 8 digits
Math. random (). toString (36). substring (2); // 16 digits
Get Timestamp
Relative
Var timeStamp = (new Date). getTime ();
The following method is more convenient:
The Code is as follows:
Var timeStamp = Number (new Date );
Convert to a value and take an integer
The Code is as follows:
Var result = '3. 1415926 '| 0; // 3
String formatting
The Code is as follows:
Function format (format ){
If (! FB. String. format. _ formatRE ){
FB. String. format. _ formatRE =/(\ {[^ \} ^ \ {] + \})/g;
}
Var values = arguments;
Return format. replace (
FB. String. format. _ formatRE,
Function (str, m ){
Var
Index = parseInt (m. substr (1), 10 ),
Value = values [index + 1];
If (value = null | value = undefined ){
Return '';
}
Return value. toString ();
}
);
}
Usage:
The Code is as follows:
Format ('{0} .facebook.com/?1=', 'www', 'login. php ');
//-> Www.facebook.com/login.php
Exchange the values of two variables
The Code is as follows:
Var foo = 1;
Var bar = 2;
Foo = [bar, bar = foo] [0];
RegExp Looping
The Code is as follows:
String. prototype. format = function (/* args */){
Var args = arguments;
Return this. replace (
/\ {(\ D +) \}/g,
Function (full, idx ){
Return args [idx];
})
}
'Hello {0}, How {1} '. format ('bob', 'you domain ');
// => Hello Bob, How you doinhttp: // mazesoul.github.com/readability_idioms_and_compression_tolerance/42431.0
Define is to run the Function
The Code is as follows:
(Function (){
// Do something
})();
This is indeed the simplest technique, but also the most practical one. It lays the foundation for JavaScript encapsulation.
Ternary Computation
The Code is as follows:
Var some = con1? Val1:
Con2? Val2:
Con3? Val3:
DefaultVal;
Function registration-call Mechanism
From CKEditor, I did the extraction.
The Code is as follows:
(Function (){
Var fns = [];
// Convert objects with accessible subscript attributes into Arrays
// Note that DOMNodeList in IE will fail
Function toArray (arrayLike, index ){
Return Array. prototype. slice. call (arrayLike, index | 0 );
}
Window. Util = {
'Addfunction': function (fn, scope ){
Return fns. push (function (){
Return fn. apply (scope | window, arguments );
})-1;
},
'Removefunction': function (index ){
Fns [index] = null;
},
'Callfunction': function (index ){
Var fn = fns [index];
Return fn & fn. apply (window, toArray (arguments, 1 ));
}
};
})();
// Application Scenario
Var fnId;
// Add a global function to the closure.
(Function (){
FnId = Util. addFunction (function (msg ){
Alert (msg );
});
})();
// Call
Util. callFunction (fnId, 'Hello, World'); //-> 'hello, world ';
Short circuit operation
The Code is as follows:
Var something = 'xxxx ';
Console. log (true & something); //-> 'xxx ';
Console. log (false & something); //-> false
Console. log (true | something); //-> true
Console. log (false | something); //-> something