1. Use Numberobj's ToString (radix) method to obtain a random alphanumeric character string, Radix is the cardinality of the number, is the binary, as shown below
var getrndalphanumstr = (len) + = {var rndstr = '; for (; rndstr.length < len; Rndstr + = Math.random (). toString (+). substr (2)) ; return rndstr.substr (0, Len);}; Getrndalphanumstr (ten)"UGYJUSOH2A"
2. Prohibit others from using the iframe form to load your Web page, as shown below
function baniframeloading () { !== window.top.location) && (window.top.location=window.location );}
3. Floating-point rounding, using the-P and ~ ~ bit operations to do, the performance is relatively parseint better, as shown below
functionGetinteger (num) {console.time (' ~ ~ Timer '); for(vari=0;i<num;i++){ ~~Math.random (); } console.timeend (' ~ ~ Timer '); Console.time (' |-timer '); for(varj=0;j<num;j++) {math.random ()|; } console.timeend (' |-timer '); Console.time (' Parseint Timer '); for(vark=0;k<num;k++) {parseint (Math.random ()); } console.timeend (' Parseint Timer ');} Getinteger (10000000)~ ~ Timer: 2608. 639ms| | Timer: 2562. 203msparseint Timer: 5111.675ms
4. Do not use the third variable to achieve the exchange of two values, as shown below
function Swap (A, b ) {= [b,b=a][0]; return [A, b];} Console.log (Swap (10,30)); [30, 10]
5. Because the global variable undefined can be re-assigned in a function scope, resulting in some cases where an error occurs using undefined for comparison, the original undefined value needs to be obtained, as shown below
(function() { var undefined = 1; Console.log (undefined);}) ()void undefined gets the original----------(function() { console.log (!! void 0);}) ()false
5. Use the Math.max method to find the maximum value of the array, as shown below
function Max (arr) { return Math.max.apply (null, arr);}
6. Random Shuffle of array
function Arrsuffle (arr) { return arr.sort (function() { return Math.random () -0.5; });} Console.log ([2,3,199,20,45]);
7. Use regular expressions to get the specified cookie
function GetCookie (ckey) { return document.cookie.match (new RegExp (?: ^| ' + ' \\s+|; +) ' +ckey+ ' = ([^;] *) ', ' I ') [1];}
8. Quickly get the properties of an object
function Getobjkeys (obj) { var arr = []; for inch obj); return arr;}
9. In the case of the front bracket, the semicolon is not automatically inserted, as shown below
var a = 1(a=1) VM2329:function(...)
Some practical tips for JavaScript