javascript学习笔记(八) js内置对象
更新时间:2012年06月19日 23:11:39 作者:
javascript学习笔记之js内置对象,需要的朋友可以参考下
1.URI方法
encodeURI()和encodeURIComponent()对URI进行编码
encodeURI()不会对本身属于URI的特殊字符进行编码,如冒号,正斜杠,问好,井字等
encodeURIComponent()会对任何非标准字符进行编码
2.eval() 方法:解释参数中的代码字符串
var msg = "hello world";
eval("alert(msg)"); //"hello world"
3.Math 对象
Math.E 数学中的e的值
Math.PI π的值
Math.SQRT2 2的平方根
Math.abs(num) num的绝对值
Math.exp(num) e的num次幂
Math.log(num) num的自然对数
Math.pow(num,n) num的n次幂
Math.sqrt(num) num的平方根
Math.acos(x) x的反余弦值
Math.asin(x) x的反正弦值
Math.atan(x) x的反正切值
Math.atan2(y,x) y/x的反正切值
Math.cos(x) x的余弦值
Math.sin(x) x的正弦值
Math.tan(x) x的正切值
4.min()和max()方法
var max = Math.max(3,45,67,32);
alert(max); //67
var min = Math.min(2,46,74);
alert(min); //2
5.小数舍入到整数方法
Math.ceil() 向上舍入
Math.floor() 向下舍入
Math.round() 四舍五入
alert(Math.ceil(25.1)); //26
alert(Math.ceil(25.5)); //26
alert(Math.ceil(25.9)); //26
alert(Math.round(25.1)); //25
alert(Math.round(25.5)); //26
alert(Math.round(25.9)); //26
alert(Math.floor(25.1)); //25
alert(Math.floor(25.5)); //25
alert(Math.floor(25.9)); //25
6. random() 方法 返回 介于 0~1 的一个随机数,不包括0和1
在某一范围内取一个随机数公式:
随机数 = Math.floor(Math.random * 总数 + 第一个值) // 总数=第二个值 - 第一个值
//取范围内随机数函数
function selectFrom(lowerValue,upperValue) {
var count = upperValue - lowerValue + 1;
return Math.floor(Math.random() * count +lowerValue);
}
var num = selectFrom(2,10);
alert(num); //介于2~10之间的数(包括2和10)
encodeURI()和encodeURIComponent()对URI进行编码
encodeURI()不会对本身属于URI的特殊字符进行编码,如冒号,正斜杠,问好,井字等
encodeURIComponent()会对任何非标准字符进行编码
2.eval() 方法:解释参数中的代码字符串
复制代码 代码如下:
var msg = "hello world";
eval("alert(msg)"); //"hello world"
3.Math 对象
Math.E 数学中的e的值
Math.PI π的值
Math.SQRT2 2的平方根
Math.abs(num) num的绝对值
Math.exp(num) e的num次幂
Math.log(num) num的自然对数
Math.pow(num,n) num的n次幂
Math.sqrt(num) num的平方根
Math.acos(x) x的反余弦值
Math.asin(x) x的反正弦值
Math.atan(x) x的反正切值
Math.atan2(y,x) y/x的反正切值
Math.cos(x) x的余弦值
Math.sin(x) x的正弦值
Math.tan(x) x的正切值
4.min()和max()方法
复制代码 代码如下:
var max = Math.max(3,45,67,32);
alert(max); //67
var min = Math.min(2,46,74);
alert(min); //2
5.小数舍入到整数方法
Math.ceil() 向上舍入
Math.floor() 向下舍入
Math.round() 四舍五入
复制代码 代码如下:
alert(Math.ceil(25.1)); //26
alert(Math.ceil(25.5)); //26
alert(Math.ceil(25.9)); //26
alert(Math.round(25.1)); //25
alert(Math.round(25.5)); //26
alert(Math.round(25.9)); //26
alert(Math.floor(25.1)); //25
alert(Math.floor(25.5)); //25
alert(Math.floor(25.9)); //25
6. random() 方法 返回 介于 0~1 的一个随机数,不包括0和1
在某一范围内取一个随机数公式:
随机数 = Math.floor(Math.random * 总数 + 第一个值) // 总数=第二个值 - 第一个值
复制代码 代码如下:
//取范围内随机数函数
function selectFrom(lowerValue,upperValue) {
var count = upperValue - lowerValue + 1;
return Math.floor(Math.random() * count +lowerValue);
}
var num = selectFrom(2,10);
alert(num); //介于2~10之间的数(包括2和10)
相关文章
JavaScript 输出显示内容(document.write、alert、innerHTML、console.log
刚开始接触js的朋友肯定要接触到js的输出,那么肯定会使用document.write、alert、innerHTML、console.log,这里简单介绍一下,需要的朋友可以参考一下啊2016-12-12getElementById().innerHTML与getElementById().value的区别
这篇文章主要介绍了getElementById().innerHTML与getElementById().value的区别,因为经常有新手朋友问到,特整理一下,需要的朋友可以参考下2016-10-10温故知新——JavaScript中的字符串连接问题最全总结(推荐)
这篇文章主要介绍了JS中的字符串连接问题,ECMAScript 中最常见的一个问题是字符串连接的性能。在调用 join() 方法时才会发生连接操作,具体操作步骤大家可查看下文的详细讲解,感兴趣的小伙伴们可以参考一下。2017-08-08
最新评论