1. 取整同時轉成數值型:數組
- '10.567890'|0
- //結果: 10
- '10.567890'^0
- //結果: 10
- -2.23456789|0
- //結果: -2
- ~~-2.23456789
- //結果: -2
2. 日期轉數值:app
- var d = +new Date(); //1295698416792
3. 類數組對象轉數組:dom
- var arr = [].slice.call(arguments)
4. 漂亮的隨機碼:post
- Math.random().toString(16).substring(2); //14位
- Math.random().toString(36).substring(2); //11位
5. 合併數組:spa
- 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. 用0補全位數:prototype
- function prefixInteger(num, length) {
- return (num / Math.pow(10, length)).toFixed(length).substr(2);
- }
7. 交換值:對象
- a= [b, b=a][0];
8. 將一個數組插入另外一個數組的指定位置:blog
- 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. 刪除數組元素:get
- var a = [1,2,3,4,5];
- a.splice(3,1);
10. 快速取數組最大和最小值string
- Math.max.apply(Math, [1,2,3]) //3
- Math.min.apply(Math, [1,2,3]) //1
11. 條件判斷:
- var a = b && 1;
- //至關於
- if (b) {
- a = 1;
- } else {
- a = b;
- }
- var a = b || 1;
- //至關於
- if (b) {
- a = b;
- } else {
- a = 1;
- }
12. 判斷IE(兩種方法):
- var ie = /*@cc_on !@*/false;
- var ie = !-[1,];