Here are 12 useful Javascript tricks to share . 16 years after JavaScript was born in 1995, countless web pages around the world rely on her for mission-critical tasks, andJavaScript has ranked 8th in the list of programming languages published by Tiobe, followed by C #, JavaScript has shifted from a previously decorative scripting language to a mainstream programming language that people use to develop larger and more complex programs.
1. Turn the rounding into a numerical type:
' 10.567890 ' |
Results: 10
' 10.567890 ' ^0
Results: 10
-2.23456789|0
Results: 2
~~-2.23456789
Results: 2
2. Date to Value:
var d = +new Date (); 1295698416792
3. Class Array Object goto array:
var arr = [].slice.call (arguments)
4. Beautiful Random Code:
Math.random (). toString (+). substring (2); 14-bit Math.random (). toString (+). substring (2); 11 Guests
5. Merging arrays:
var a = [1,2,3];var B = [4,5,6]; Array.prototype.push.apply (A, b); Uneval (a); [1,2,3,4,5,6]
6. Use 0 full-complement digits:
function Prefixinteger (num, length) { return (Num/math.pow (ten, length)). toFixed (length). substr (2);
7. Exchange Value:
a= [B, b=a][0];
8. Insert an array into another array at the specified location:
var a = [1,2,3,7,8,9];var b = [4,5,6];var Insertindex = 3;a.splice.apply (A, Array.concat (Insertindex, 0, b));//a:1,2,3,4 , 5,6,7,8,9
9. Delete the array element:
var a = [1,2,3,4,5];a.splice (3,1);
10. Quickly take the maximum and minimum values of the array
Math.max.apply (math, [])//3math.min.apply (math, [])//1
11. Condition Judgment:
var a = b && 1;
Equivalent
if (b) { a = 1}
var a = B | | 1;
Equivalent
if (b) { a = b;} else { a = 1;}
12. Determine IE:
var ie =/* @cc_on [email protected]*/false;
12 Practical Javascript tricks for kinky sex