首頁  >  文章  >  web前端  >  javascript學習筆記(八) js內建物件_基礎知識

javascript學習筆記(八) js內建物件_基礎知識

WBOY
WBOY原創
2016-05-16 17:52:26846瀏覽

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 = upper>var count = upperValue lowerValue 1; return Math.floor(Math.random() * count lowerValue); } var num = selectFrom(2,10); alert(num); / /介於2~10之間的數字(包括2和10)
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn