內置對象javascript
1.數組對象java
1)數組的聲明正則表達式
①方式一:數組
var arr=new Array(); //聲明一個數組dom
arr[0]=100; //向數組中添加值函數
arr[1]=200;字體
//arr['name']="autumn"; //JS的數組不支持關聯數組url
document.write(arr); //100,200spa
document.write(arr.length); //打印數組的元素個數prototype
document.write(arr[0]); //打印數組的某一個元素
②方式二:
var arr=new Array(100,200,300);
document.write(arr); //100,200,300
③方式三:
var arr=new Array(3); //至少有3個數組元素
arr[0]=100;
document.write(arr); //100,,
④方式四:(推薦使用)
var arr=[100,200,300];
document.write(arr); //100,200,300
2)數組的屬性
array.constructor 對象的類型
array.length 數組的元素個數
Array.prototype.成員屬性/成員方法=屬性值/function(){} 爲內置數組對象添加新的屬性或方法
3)數組的方法
concat() 鏈接兩個或多個數組生成新數組
arr.concat(arr1,arr2,num1,num2,...)
參數能夠是數組也能夠是具體數值
例:
var arr1=[100,200,300];
var arr2=[400,500];
var arr3=[600,700];
document.write(arr1.concat(arr2,arr3,800)); //100,200,300,400,500,600,700,800
join() 將數組元素鏈接成字符串
arr.join("指定分隔符")
參數省略,則默認使用逗號做爲分隔符
例:
var arr=[100,200,300];
document.write(arr.join("-")); //100-200-300
pop() 刪除並返回數組的最後一個元素
arr.pop()
例:
var arr=[100,200,300];
document.write(arr.pop()); //300
document.write(arr); //100,200
shift() 刪除並返回數組的第一個元素
arr.shift()
例:
var arr=[100,200,300];
document.write(arr.shift()); //100
document.write(arr); //200,300
push() 在數組尾部添加一個或多個元素
arr.push(element1,element2,...)
例:
var arr=[100,200,300];
document.write(arr.push(400,500)); //5,新的元素個數
document.write(arr); //100,200,300,400,500
unshift() 在數組頭部添加元素
arr.unshift(element1,element2,...)
例:
var arr=[100,200,300];
document.write(arr.unshift(400,500)); //5,新的元素個數
document.write(arr); //400,500,100,200,300
slice() 返回數組中選定部分的元素
arr.slice(start,end)
參數start:起始位置,數組頭部從0開始,若爲負則從尾部開始,-1對應最後一個元素
參數end:可選參數,結束位置,若爲負數則從尾部開始,若未指定則直到數組結束
例:
var arr=[100,200,300,400,500];
document.write(arr.slice(1)); //200,300,400,500
document.write(arr.slice(2,4)); //300,400
splice() 刪除、替換或插入數組元素
arr.splice(index,num,val1,val2,...)
參數index:必需,指定要添加/刪除元素的位置,若爲負數則從尾部開始
參數num:必需,要刪除的元素數量,爲0則不刪除
參數val1,val2,...:可選,向數組添加的新元素
例:
var arr=[100,200,300];
document.write(arr); //100,200,300
arr.splice(1,0,666);
document.write(arr); //100,666,200,300
arr.splice(1,3,999);
document.write(arr); //100,999
arr.splice(1,1,200);
document.write(arr); //100,200
reverse() 顛倒數組中的元素順序
arr.reverse()
例:
var arr=[100,200,300];
document.write(arr.reverse()); //300,200,100
sort() 將數組元素排序
arr.sort(function)
參數function:可選參數,不指定該參數則按天然順序排序,若要指定則該參數必須是函數
例:
var arr=[10,5,40,25,1000,1];
document.write(arr.sort()); //1,10,1000,25,40,5
function sortNumber(a,b){
return a-b;
}
document.write(arr.sort(sortNumber)); //1,5,10,25,40,1000
toString() 將數組轉爲字符串
arr.toString() 與無參數的join()方法返回的字符串相同
例:
var arr=[100,200,300];
document.write(arr.toString()); //"100,200,300"
2.布爾對象
true false
3.日期對象
1)日期的建立
var date=new Date();
document.write(date);
2)日期的屬性
date.constructor
Date.prototype
3)日期的方法
getDate() 返回一個月中的第幾天(1~31)
例:
var date=new Date();
document.write(date.getDate());
getDay() 返回一週中的第幾天(0~6)
getMonth() 返回月份(0~11)
例:
document.write(date.getMonth()+1);
getFullYear() 返回四位數字的年份
例:
var date=new Date();
document.write(date.getFullYear());
var d=new Date("July 21,1990 12:00:00");
document.write(d.getFullYear()); //1990
getHours() 返回小時(0~23)
getMinutes() 返回分鐘(0~59)
getSeconds() 返回秒數(0~59)
例:
var date=new Date("July 21,1990 12:00:00");
document.write(date.getHours()); //12
document.write(date.getMinutes()); //0
document.write(date.getSeconds()); //0
getMilliseconds() 返回毫秒數(0~999)
getTime() 返回1970年1月1日至今的毫秒數
parse() 返回1970年1月1日至指定日期(字符串)的毫秒數
例:
var date=Date.parse("July 8,2005");
document.write(date);
getTimezoneOffset() 返回本地時間與格林威治標準時間(GMT)的時差,以分鐘爲單位
例:
var date=new Date();
document.write(date.getTimezoneOffset()/60);
4)計時器
<div id="clock"></div> <input type="button" id="btn" value="Start"> <script type="text/javascript"> window.onload=function(){ var clock=document.getElementById("clock"); var getDate=function(){ var date=new Date(); //函數體內使用var聲明的變量爲局部變量 var strDate=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds(); clock.innerHTML=strDate; } getDate(); var btn=document.getElementById("btn"); var n=true; btn.onclick=function(){ if(n){ btn.value="Stop"; getDate(); //函數體內不使用var聲明的變量爲全局變量 //setInterval開始計時器,每隔多少毫秒運行一次 interval=setInterval(getDate(),1000); n=false; }else{ btn.value="Start"; //clearInterval清除計數器 clearInterval(interval); interval=null; n=true; } } } </script>
4.數學對象
1)數學對象屬性
Math.PI 圓周率
2)數學對象方法
abs() 返回數的絕對值
Math.abs(num)
例:
document.write(Math.abs(-0.01)); //0.01
ceil() 進一法取整
Math.ceil(num)
例:
document.write(Math.ceil(0.40)); //1
document.write(Math.ceil(-5.9)); //-5
floor() 捨去法取整
Math.floor(num)
例:
document.write(Math.floor(0.60)); //0
document.write(Math.floor(-5.1)); //-6
max() 返回最大值
Math.max(num1,num2,...)
例:
document.write(Math.max(5,-3,7,0.5,-1);
min() 返回最小值
Math.min(num1,num2,...)
pow(x,y) 返回x的y次冪的值
Math.pow(x,y)
random() 返回0~1之間的一個隨機數
Math.random()
round() 返回四捨五入後的整數
Math.round(num)
sqrt() 返回一個大於等於0的數的平方根
Math.sqrt(num)
參數num必須大於等於0
5.數值對象
直接使用數字就行,無須使用該對象
數值對象的方法:
toFixed() 將數值四捨五入爲指定小數位數的數字,不足位數的用0補
num.toFixed(指定小數位數)
參數不指定則默認爲0,最大實現位數範圍爲0~20
6.字符串對象
1)字符串的屬性
str.length 字符串的長度
例:
var str="abc123";
document.write(str.length); //6
2)字符串的方法
①字符串的顯示
anchor() 建立HTML錨點
str.anchor("錨點名稱")
例:
var str="Hello world";
document.write(str.anchor("top")); //<a name="top">Hello world</a>
big() 用大號字體顯示字符串
small() 用小號字體顯示字符串
例:
var str="Hello world";
document.write(str.big());
document.write(str.small());
bold() 用粗體顯示字符串
italics() 用斜體顯示字符串
link() 將字符串顯示爲超連接
str.link(url)
strike() 使用刪除線顯示字符串
sub() 將字符串顯示爲下標
sup() 將字符串顯示爲上標
toLowerCase() 將字符串轉換爲小寫
toUpperCase() 將字符串轉換爲大寫
fontcolor() 以指定顏色來顯示字符串
str.fontcolor(color)
參數color:顏色名、rgb值、十六進制值
fontsize() 以指定字體大小(1~7)來顯示字符串
str.fontsize(size)
參數size:1~7的數字
②字符串的鏈接、比較
concat() 鏈接兩個或多個字符串,做用等同於「+」
str.concat(str1,str2,...)
例:
var str1="Hello";
var str2="world";
document.write(str1.concat(str2)); //Hello world
localeCompare() 比較兩個字符串
str1.localeCompare(str2)
若str1小於str2,則返回小於0的數;若str1大於str2,則返回大於0的數;若str1與str2兩個字符串相等,則返回0
③字符串的查找、匹配
charAt() 返回字符串中指定位置的字符
str.charAt(index)
參數index:必需,指定位置下標,從0開始
例:
var str="Hello world";
document.write(str.charAt(1)); //e
charCodeAt() 返回字符串中指定位置的字符對應的ASCII碼值
str.charCodeAt(idnex)
fromCharCode() 由一個或多個ASCII碼值返回對應字符組成的字符串
String.fromCharCode(num1,num2,...) 該方法是String的靜態方法
例:
document.write(String.fromCharCode(65,66,67)); //ABC
indexOf() 返回某字符段在字符串中首次出現的位置,未出現則返回-1
str.indexOf(searchstr,index)
參數searchstr:必需,要檢索的字符段
參數index:可選,默認從位置0開始檢索,指定開始檢索的位置,取值範圍爲0~str.length-1
例:
var str="Hello world";
document.write(str.indexOf("o")); //4
document.write(str.indexOf("c")); //-1
lastIndexOf() 返回某字符段在字符串中最後出現的位置,未出現則返回-1
str.lastIndexOf(searchstr,index)
參數searchstr:必需,要檢索的字符段
參數index:可選,默認從位置0開始檢索,指定開始檢索的位置,取值範圍爲0~str.length-1
例:
var str="Hello world";
document.write(str.lastIndexOf("o")); //7
match() 在字符串中匹配指定的字符或指定的正則表達式,以數組形式返回匹配到的結果,未匹配到則返回null
str.match("要匹配的字符") 或 str.match(正則表達式)
例:
var str="Hello world! 1 plus 2 equal 3";
document.write(str.match("world")); //world
document.write(str.match("haha")); //null
document.write(str.match(/\d+/g)); //1,2,3
replace() 替換掉字符串中與指定的匹配字符或正則表達式相匹配的內容,返回替換後的新字符串
str.replace("要匹配的字符"/正則表達式,"替換文本")
例:
var str="hello everybody,hello world,hello myself!";
document.write(str.replace("hello","bye")); //bye everybody,hello world,hello myself!
document.write(str.replace(/hello/,"bye")); //bye everybody,hello world,hello myself!
document.write(str.replace(/hello/g,"bye")); //bye everybody,bye world,bye myself!
document.write(str.replace(/\b\w+\b/g,function(word){return word.substring(0,1).toUpperCase()+word.substring(1);}));
//Hello Everybody,Hello World,Hello Myself!
search() 返回字符串中與指定的匹配字符或正則表達式首次匹配的位置,未匹配則返回-1
str.search("要匹配的字符"/正則表達式)
例:
var str="ok,hello everybody,hello world!";
document.write(str.search("hello")); //3
document.write(str.search(/Hello/)); //-1
document.write(str.search(/Hello/i)); //3
④字符串的截取、分割
slice() 截取字符串
str.slice(start,end)
參數start:必需,開始截取的下標位置,若爲負數則從尾部開始
參數end:可選,截取結束的下標位置,若未指定該參數則從start位置截取到字符串結束;若爲負數則從尾部開始,該位置對應的字符不被截取
例:
var str="Hello world";
document.write(str.slice(6)); //world
document.write(str.slice(6,-1)); //worl
substr() 截取指定長度的字符
str.substr(start,length)
參數start:必需,開始截取的下標位置,若爲負數則從尾部開始
參數length:可選,要截取的字符長度,若未指定該參數,則從start位置截取至字符串結束
例:
var str="Hello world";
document.write(str.substr(6)); //world
document.write(str.substr(6,1)); //w
substring() 截取字符串
str.substring(start,end)
參數start:必需,非負的整數,開始截取的下標位置
參數end:可選,非負的整數,截取至該位置對應的字符,但不包括該字符;若未指定該參數,則從start位置截取至字符串結束
例:
var str="Hello world";
document.write(str.substring(6)); //world
document.write(str.substring(6,7)); //w
split() 分割字符串爲字符段數組
str.split("分隔符",返回的數組的最大長度)
參數"分隔符":必需,爲字符串或正則表達式
參數「返回的數組的最大長度」:可選,若未設置該參數則返回全部分割後的字符段;若指定該參數,則返回的字符段數組元素不會多於這個指定數
例:
var str="Hello world";
document.write(str.split(" ")); //Hello,world
document.write(str.split("")); //H,e,l,l,o, ,w,o,r,l,d
document.write(str.split(" ",1)); //Hello
document.write(str.split("",1)); //H
7.正則表達式對象
/正則表達式/修飾符 (注意:正則表達式不加引號)
1)修飾符
i 不區分大小寫地匹配
g 全局匹配(查找全部匹配而非在找到第一個匹配後就中止)
m 執行多行匹配
2)正則表達式的方法
exec() 檢索字符串中與正則表達式相匹配的值,以數組形式返回匹配到的結果,未匹配到則返回null
例:
var str="Hello world";
var pattern=/world/g;
document.write(pattern.exec(str)); //world
注意:exec()在完成一次匹配後指針會向後移動到匹配位置,若要接着開始從新檢索字符串,須手動將 lastIndex 屬性重置爲0
test() 檢索字符串中與正則表達式相匹配的值,返回true或false
例:
var str="Hello world";
var pattern=/world/g;
document.write(pattern.test(str)); //true