常用內置函數(註意大小寫)
常用內置函數(註意大小寫)
1.Math數學對象
a) Math對象常用屬性
屬性 |
說明 |
Math.E |
歐拉常數 |
Math.LN2 |
2的自然對數 |
Math.LN10 |
10的自然對數 |
Math.LOG2E |
基數為2的對數 |
Math.LOG10E |
基數為10的對數 |
Math.PI |
圓周率 |
Math.SQRT1_2 |
0.5的平方根 |
Math.SQRT2 |
2的平方根 |
b) Math對象常用內部函數
函數名 |
說明 |
Math.max(arg1,arg2) |
求最大值 |
Math.min(arg1,arg2) |
求最小值 |
Math.pow(arg1,arg2) |
arg1的arg2次方 |
Math.sqrt(arg) |
求平方根 |
Math.random() |
求0~10的隨機數 |
Math.round(arg) |
四舍五入 |
Math.sin(arg) |
求正弦 |
Math.cos(arg) |
求余弦 |
Math.tan(arg) |
求正切 |
Math.abs(arg) |
求絕對值 |
2.Date日期對象(需要詳細說明)
a) 定義一個Date對象
b) 調用Date對象的構造函數得到相應的日期對象
var time=new Date(),沒有參數的構造函數得到當前時間多的Date對象
Date對象的方法
函數名 |
說明 |
getYear()/setYear() |
返回/改變年份 |
getMonth/setMonth() |
返回/改變月份--從0開始,要+1 |
getDate()/setDate() |
返回/改變日期 |
getDay()/setDay() |
返回/改變星期 |
getHours()/setHours() |
返回/改變小時 |
getMinutes()/setMinutes() |
返回/改變分鐘 |
getSeconds()/setSeconds() |
返回/改變秒 |
getTime()/setTime() |
返回/改變完整時間 |
3.編碼函數escape和unescape
a) 在實際中,漢字編碼容易出現亂碼現象,使用escape()可避免出現亂碼現象
b) escape()編碼
c) unescape()解碼
4.動態執行代碼eval
例:
var str=”windows.alert(“使用eval函數運行”);
document.write(“生成的字符串:”+str);
eval(str);
5.判斷是否為數值類型 isNaN()
常用內置函數(註意大小寫)