1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
1. 取整同时转成数值型: <strong>'10.567890'|0</strong> 结果: 10 <strong>'10.567890'^0</strong> 结果: 10 <strong>-2.23456789|0</strong> 结果: -2 <strong>~~-2.23456789</strong> 结果: -2 2. 日期转数值: <strong>var d = +new Date(); //1295698416792</strong> 3. 类数组对象转数组: <strong>var arr = [].slice.call(arguments)</strong> 4. 漂亮的随机码: <strong>Math.random().toString(16).substring(2); //14位</strong> <strong>Math.random().toString(36).substring(2); //11位</strong> 5. 合并数组: <strong>var a = [1,2,3];</strong> <strong>var b = [4,5,6];</strong> <strong>Array.prototype.push.apply(a, b);</strong> <strong>uneval(a); //[1,2,3,4,5,6]</strong> 6. 用0补全位数: <strong>function prefixInteger(num, length) {</strong> <strong> return (num / Math.pow(10, length)).toFixed(length).substr(2);</strong> <strong>}</strong> 7. 交换值: a= [b, b=a][0]; 8. 将一个数组插入另一个数组的指定位置: <strong>var a = [1,2,3,7,8,9];</strong> <strong>var b = [4,5,6];</strong> <strong>var insertIndex = 3;</strong> <strong>a.splice.apply(a, Array.concat(insertIndex, 0, b));</strong> <strong>// a: 1,2,3,4,5,6,7,8,9</strong> 9. 删除数组元素: <strong>var a = [1,2,3,4,5];</strong> <strong>a.splice(3,1);</strong> 10. 快速取数组最大和最小值 <strong>Math.max.apply(Math, [1,2,3]) //3</strong> <strong>Math.min.apply(Math, [1,2,3]) //1</strong> 11. 条件判断: <strong>var a = b && 1; </strong> <strong>相当于</strong> <strong>if (b) {</strong> <strong> a = 1</strong> <strong>}</strong> <strong>var a = b || 1; </strong> <strong>相当于</strong> <strong>if (b) {</strong> <strong> a = b;</strong> <strong>} else {</strong> <strong> a = 1;</strong> <strong>}</strong> 12. 判断IE: <strong>var ie = /*@cc_on !@*/false;</strong> |
你不知道的javascript用法
转载原创文章请注明,转载自: 前端扫地生 » 你不知道的javascript用法
发表评论