当前位置:编程学习 > JAVA >>

javascript学习笔记(八)内置对象

1.URI方法
encodeURI()和encodeURIComponent()对URI进行编码
encodeURI()不会对本身属于URI的特殊字符进行编码,如冒号,正斜杠,问好,井字等
encodeURIComponent()会对任何非标准字符进行编码

2.eval() 方法:解释参数中的代码字符串

1 var msg = "hello world";
2 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()方法

1 var max = Math.max(3,45,67,32);
2 alert(max);        //67
3 var min = Math.min(2,46,74);
4 alert(min);        //25.小数舍入到整数方法
Math.ceil() 向上舍入
Math.floor() 向下舍入
Math.round() 四舍五入


 1 alert(Math.ceil(25.1));        //26
 2 alert(Math.ceil(25.5));        //26
 3 alert(Math.ceil(25.9));        //26
 4
 5 alert(Math.round(25.1));        //25
 6 alert(Math.round(25.5));        //26
 7 alert(Math.round(25.9));        //26
 8
 9 alert(Math.floor(25.1));        //25
10 alert(Math.floor(25.5));        //25
11 alert(Math.floor(25.9));        //25
6. random() 方法 返回 介于 0~1 的一个随机数,不包括0和1
在某一范围内取一个随机数公式:
随机数 = Math.floor(Math.random * 总数 + 第一个值)        // 总数=第二个值 - 第一个值


1 //取范围内随机数函数
2 function selectFrom(lowerValue,upperValue) {
3     var count = upperValue - lowerValue + 1;
4     return Math.floor(Math.random() * count +lowerValue);
5 }
6
7 var num = selectFrom(2,10);
8 alert(num);        //介于2~10之间的数(包括2和10)

 

 

摘自 晴天漫步

补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,