javascript原生方法 发表于 2017-06-06 | Edited on 2018-10-29 | 分类于 javascript | Comments: | Views: 一些常用的原生方法整理说明 methods对string操作的方法1234567891011121314151617var s = "hello,world"; //定义一个字符串s.charAt(0) // => "h" 第一个字符s.charAt(s.length-1) // => "d" 最后一个字符s.substring(1,4) // => "ell" 截取2~4的字符 s.substr(1) // => "e" 第一个字符s.slice(1,4) // => "ell" 截取2~4的字符s.slice(-3) // => "rld" 截取后3个字符s.indexOf("l") // => 2 获取第一个"l"的索引s.lastIndexOf("l") // => 10 获取最后一个"l"的索引s.indexOf("l",3) // => 3 获取第三个"l"的索引s.split(",") // => ["hello","world"] 分割字符串为数组s.replace("h","H") // => "Hello,world" 替换字符s.toUpperCase() // => "HELLO,WORLD" 大写ES5以后支持s[0] // => "h" 第一个字符 数学函数Math123456789101112131415161718192021Math.random() // 返回 0 ~ 1 之间的随机数Math.round(1.2) // => 1 把数四舍五入为最接近的整数Math.ceil(1.2) // => 2 对数进行上舍入Math.floor(1.2) // => 1 对数进行下舍入Math.max(2,4) // => 4 返回 2 和 4 中的最高值Math.min(2,4) // => 2 返回 2 和 4 中的最低值Math.abs(-2) // => 2 -2的绝对值Math.sqrt(4) // => 2 返回 4 的平方根Math.pow(2,4) // => 16 返回 2 的 4 次幂Math.exp(0) // => 1 返回 e 的指数Math.log(1) // => 1 返回数的自然对数(底为e)Math.cos(0) // => 1 返回0的余弦Math.acos(1) // => 0 返回1的反余弦值Math.asin(0) // => 0 返回0的反正弦值Math.sin(0) // => 0 返回0的正弦Math.tan(0) // => 0 返回角的正切Math.atan(0) // => 0 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 0 的反正切值Math.atan2(0,1) // => 0 返回从 x 轴到点 (0,1) 的角度(介于 -PI/2 与 PI/2 弧度之间)Math.toSource() // 返回该对象的源代码Math.valueOf() // 返回 Math 对象的原始值 打賞 微信支付