內置對象,原聲對象的差別:
前者總是在引擎初始化階段就被創建好的對象,是后者的一個子集;而后者包括了一些在運行過程中動態創建的對象。
數組: ?http://www.cnblogs.com/maplejan/archive/2013/03/12/2954908.html
pop 和 push
Array.pop();????// 刪除數組最后一個元素,返回被刪除的元素
Array.push(element1, ..., elementN);? ? // 在數組尾部插入1-N個元素,返回操作后數組的length
通過這?pop 和 push ,就能把數組模擬成 堆棧(stack) 來進行操作。
堆棧這種數據結構的特點,就是“后進先出”(LIFO, Last In First Out)。
shift 和 unshift
Array.shift();? ? //刪除數組第一個元素,返回被刪除的元素
Array.unshift(element1, ..., elementN) ;// 在數組頭部插入1-N個元素,返回操作后數組的length
利用?shift 和 unshift 則可以實現 隊列(queue) 的操作。
隊列的操作方式和堆棧相反,采用“先進先出”(FIFO, First-In-First-Out)。
splice
Array.splice(index , howMany[, element1[, ...[, elementN]]]);
Array.splice(index);
參數:
index:規定從何處添加/刪除元素。
howmany:規定應該刪除多少元素。
elements:規定要添加到數組的新元素,從 index 所指的下標處開始插入。
splice方法是對?pop、push、shift、unshift 的一個補充。
返回值是被刪除的元素。
reverse
Array.reverse();? ? // 顛倒數組中元素的順序,并返回逆序后的數組
sort
Array.sort([compareFunction]);
如果調用該方法時沒有使用參數,將按字母順序對數組中的元素進行排序。
說得更精確點,是按照字符編碼的順序進行排序。
如果想按照其他標準進行排序,就需要提供比較函數,該函數要比較兩個值,然后返回一個用于說明這兩個值的相對順序的數字。比較函數應該具有兩個參數 a 和 b,其返回值如下:
若 a 小于 b,在排序后的數組中 a 應該出現在 b 之前,則返回一個小于 0 的值。
若 a 等于 b,則返回 0。
若 a 大于 b,則返回一個大于 0 的值。
訪問方法(Accessor methods)
這些方法只是返回相應的結果,而不會修改數組本身
concat
Array.concat(value1, value2, ..., valueN);? ? // 鏈接2個或多個數組,并返回合并后的數組
但有一個需要注意的地方,用下面的例子說明:
1vararr = [1, 2, 3];2arr.concat(4, 5);//return [1, 2, 3, 4, 5]3arr.concat([4, 5]);//return [1, 2, 3, 4, 5]4arr.concat([4, 5], [6, 7]);//return [1, 2, 3, 4, 5, 6, 7]5arr.concat(4, [5, [6, 7]]);//return [1, 2, 3, 4, 5, [6, 7]]
join
string = Array.join(separator);
把數組中的所有元素放入一個字符串。其中,元素之間是通過指定的分隔符進行分隔的。
默認的分隔符是逗號(,),返回值是合并后字符串。
1[1, 2, 3].join();//return "1,2,3"
Array.join()方法,實際上是String.splite()的逆向操作。
slice
Array.slice(begin[, end]);? ? //?數組中返回選定的元素
toString
Array.toString();? ? // 這個就不說了,所有JavaScript都有toString這個方法
indexOf 和 lastIndexOf? ? *[ECMAScript 5]
Array.indexOf(searchElement[, fromIndex]);? ? // 從頭開始搜索
Array.lastIndexOf(searchElement[, fromIndex]);? ? // 從尾開始搜索
searchElement:需要搜索的值
fromIndex:索引,指示搜索從哪里開始
迭代方法(Iteration methods)
forEach*[ECMAScript 5]
Array.forEach(callback[, thisArg]);? ? // 從頭到尾遍歷一次數組,并為數組中的每個元素,調用指定的函數
參數:
callback:遍歷數組時調用的函數
thisArg:指定 callback?的作用域
另外,callback會調用三個參數:
value:數組元素
index:數組索引
array:數組本身
1[1, 2].forEach(function(value, index, array) {2console.log(value, index, array);3});4//return5//1 0 [1, 2]6//2 1 [1, 2]
Note:forEach是無法通過break來中斷數組的遍歷。
解決方法:利用try方法來拋出異常,終止遍歷。
1try{2[1,2,3].forEach(function(val) {3console.log(val);4throw(e)5});6}catch(e) {7console.log(e);8}
map*[ECMAScript 5]
Array.map(callback[, thisArg]);? ? // 遍歷數組元素,調用指定函數,并以數組返回所有結果
參數:
callback:遍歷數組時調用的函數
thisObject?:指定 callback 的作用域
例子:
1[1, 2, 3].map(function(num) {//return [2, 3, 4]2returnnum + 1;3});
filter*[ECMAScript 5]
Array.filter(callback[, thisObject]);? ? // 遍歷數組調用方法,滿足條件(返回true)的元素,將被添加到返回值的數組中
參數:
callback:遍歷數組時調用的函數
thisObject?:指定 callback 的作用域
例子:
1[1, 2, 3].filter(function(num) {//return [1]2returnnum < 2;3});
every 和?some*[ECMAScript 5]
Array.every(callback[, thisObject]);? ? // “與”
Array.some(callback[, thisObject]);? ? // “或”
參數:
callback:遍歷數組時調用的函數
thisObject:指定 callback 的作用域
every:當所有元素調用函數都返回true,結果才返回true,不然均返回false。
some:當所有元素調用函數都返回false,結果才返回false,不然均返回true。
一旦every和some的返回值確定,就會立刻停止遍歷。
例子:
1[1, 2, 3]. every(function(num) {//return false2returnnum > 1;3});4[1, 2, 3]. some(function(num) {//return true5returnnum > 2;6});
reduce 和 reduceRight*[ECMAScript 5]
Array.reduce(callback[, initialValue]);? ? // 使用指定的方法將數組元素進行組合,按索引從低到高(從左到右)
Array.reduceRight(callback[, initialValue]);? ? //?使用指定的方法將數組元素進行組合,按索引從高到低(從右到左)
參數:
callback:遍歷數組時調用的函數
initialValue:第一個次調用callback時傳入的previousValue
另外,callback會調用四個參數:
previousValue:到目前為止的操作累積結果
currentValue:數組元素
index:數組索引
array:數組本身
例子:
1[1, 2, 3]. reduce(function(x, y) {//return 1062returnx +y;3}, 100);
對象:
1.