內(nèi)置對(duì)象有
數(shù)學(xué)對(duì)象(Math對(duì)象), 日期對(duì)象(Date對(duì)象)渣刷,還有String對(duì)象
Math對(duì)象:
? ? ? ? alert(Math.PI);圓周率
? ? ? ? alert(Math.ceil(1.001));向上取整? 得到的值大于或者等于參數(shù)
? ? ? ? alert(Math.ceil(1.00));得到的值大于或者等于參數(shù)
? ? ? ? alert(Math.ceil(-1.01));得到的值大于或者等于參數(shù)
? ? ? ? alert(Math.floor(3.999));向下取整? 得到的值小于或者等于參數(shù)
? ? ? ? alert(Math.round(3.888));四舍五入
? ? ? ? alert(Math.max(2,6,7));//取參數(shù)中較大的那個(gè)數(shù)
? ? ? ? ?alert(Math.pow(2,3));//得到2的3次方(冪)
? ? ? ? ?alert(Math.random());//隨機(jī)數(shù)? [0,1);
? ? ? ? 寫1-30的隨機(jī)數(shù):alert(Math.ceil(Math.random() * 30));[1,30]
?日期對(duì)象(Date對(duì)象)
? var mydate = new Date();
? alert(typeof mydate)
?alert(mydate);包含? 星期? 月 日 年 時(shí)分秒? 時(shí)區(qū)
?alert(mydate.getFullYear());獲取年份
alert(mydate.getMonth());得到月份 取值范圍[0,11] 一月份==》0? 二月份==》1 ... 十二月份==》11
alert(mydate.getDate());得到日期
alert(mydate.getDay());星期 取值范圍[0,6] 星期日==》0 星期一==》1 ...星期六===》6
var h = mydate.getHours();時(shí)
var mi = mydate.getMinutes();分鐘
var s = mydate.getSeconds();秒
alert(mydate.getMilliseconds());
alert(mydate.getYear());
document.write(mydate.getTime() + "
");
document.write(mydate.getTime()/1000/60/60/24/365);
在設(shè)置日期對(duì)象時(shí)稽犁,把set變成get即可?
日期對(duì)象運(yùn)用的例子如下:
效果如下:
?設(shè)置定時(shí)器
? ?setInterval(參數(shù)1,參數(shù)2)
? ?參數(shù)1:js代碼,或要調(diào)用的函數(shù)
? ?參數(shù)2:交互時(shí)間? 默認(rèn)單位是毫秒
? ? ?清除定時(shí)器:
? ? clearInterval(參數(shù))
? ? 參數(shù)是設(shè)置定時(shí)器返回的id值
設(shè)置計(jì)時(shí)器
?var t = setTimeout(參數(shù)1,參數(shù)2);? 可以運(yùn)行1次
?參數(shù)1:函數(shù) 或者 js代碼
?參數(shù)2:延遲時(shí)間? 單位默認(rèn)是毫秒
?清除計(jì)時(shí)器:
? clearTimeout(t);
String對(duì)象:
創(chuàng)建字符串對(duì)象的第一種方法? ? 通過構(gòu)造函數(shù)
var str = new String("hello");
?alert(str.length);//獲取字符串的長(zhǎng)度
alert(str[4]);//索引號(hào)的取值范圍 [0,str.length-1]
? ? alert(str.charAt(2));
? ? ?alert(str.charAt(str.length-1));
? ? ?alert(str.charCodeAt(str.length-1));
? ? alert(String.fromCharCode(122));
? ? unicode編碼:a-z? ? 97--122? ? ? Unicode編碼[0,65535]
? ? A-Z? ? 65--90
? ? 數(shù)字0-9? ? 48--57
? ?字符串對(duì)象.charAt(參數(shù))? ? 通過字符串的下標(biāo)查找出字符串中的某個(gè)字符
? 參數(shù):字符串的下標(biāo)(索引號(hào)) 取值范圍:[0,字符串對(duì)象.length-1]
? ?字符串對(duì)象.charCodeAt(參數(shù))? 通過字符串的下標(biāo)查找出字符串中的某個(gè)字符對(duì)應(yīng)的Unicode編碼
? ?參數(shù):字符串的下標(biāo)(索引號(hào)) 取值范圍:[0,字符串對(duì)象.length-1]