Home  >  Article  >  Web Front-end  >  Appreciation of Javascript quatrains, some classic js codes_javascript skills

Appreciation of Javascript quatrains, some classic js codes_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:56:031053browse

1. Round and convert to numeric type:
'10.567890′|0
Result: 10
'10.567890′^0
Result: 10
-2.23456789|0
Result: -2
~~-2.23456789
Result: -2
2. Convert date to value:
var d = new Date(); //1295698416792
3. Convert array-like object to array:
var arr = [].slice.call(arguments)
4. Beautiful random code:
Math.random().toString(16).substring(2); //14 bits
Math.random().toString(36).substring(2); //11 digits
5. Merge 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 . Complete the digits with 0:
function prefixInteger(num, length) {
return (num / Math.pow(10, length)).toFixed(length).substr(2);
}
7. Swap values:
a= [b, b=a][0];
8. Insert one array into another array at the specified position:
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 array elements:
var a = [1,2,3 ,4,5];
a.splice(3,1);
10. Quickly get the maximum and minimum value of an array
Math.max.apply(Math, [1,2,3]) / /3
Math.min.apply(Math, [1,2,3]) //1
(from http://ejohn.org/blog/fast-javascript-maxmin/)
11 . Conditional judgment:
var a = b && 1;
is equivalent to
if (b) {
a = 1
}
var a = b || 1;
Equivalent to
if (b) {
a = b;
} else {
a = 1;
}
12. Determine IE:
var ie = / *@cc_on !@*/false;
Any more? Welcome to respond

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn