Object
方法 | 描述 |
---|---|
hasOwnProperty 方法 | 返回一個布爾值,該值指示某個對象是否具有指定名稱的屬性。 |
isPrototypeOf 方法 | 返回一個布爾值,該值指示某個對象是否存在於另一個對象的原型鏈中。 |
1、hasOwnProperty 方法
確定某個對象是否具有帶指定名稱的屬性
object.hasOwnProperty(proName)
參數 | 描述 |
---|---|
object | 必需。對象的實例。 |
proName | 必需。一個屬性名稱的字符串值。 |
var s = new String("Sample");
document.write(s.hasOwnProperty("split"));
document.write("<br/>");
document.write(String.prototype.hasOwnProperty("split"));
// Output:
// false
// true
var arr=[1,2];
arr.hasOwnProperty(1); //true
2、isPrototypeOf 方法
確定一個對象是否存在於另一個對象的原型鏈中。
prototype.isPrototypeOf(object)
參數 | 描述 |
---|---|
prototype | 必選。對象原型。 |
object | 必選。另一個對象,將對其原型鏈進行檢查。 |
function Rectangle() {
}
var rec = new Rectangle();
document.write(Rectangle.prototype.isPrototypeOf(rec));
// Output: true
Array
方法 | 描述 |
---|---|
concat 方法(數組) | 返回由兩個數組組合而成的新數組。 |
entries 方法 | 返回包含數組的鍵/值對的迭代器。 |
every 方法 | 檢查定義的回調函數是否爲數組中的所有元素返回 true。 |
fill 方法 | 使用指定值填充數組。 |
filter 方法 | 對數組的每個元素調用定義的回調函數,並返回回調函數爲其返回 true 的值的數組。 |
findIndex 方法 | 返回滿足回調函數中指定的測試條件的第一個數組元素的索引值。 |
forEach 方法 | 爲數組中的每個元素調用定義的回調函數。 |
indexOf 方法(數組) | 返回某個值在數組中的第一個匹配項的索引。 |
join 方法 | 返回由一個數組的所有元素串聯而成的 String 對象。 |
keys 方法 | 返回包含數組的索引值的迭代器。 |
lastIndexOf 方法(數組) | 返回指定值在數組中的最後一個匹配項的索引。 |
map 方法 | 對數組的每個元素調用定義的回調函數並返回包含結果的數組。 |
pop 方法 | 從數組中移除最後一個元素並將該元素返回。 |
propertyIsEnumerable 方法 | 返回一個布爾值,該值指示指定屬性是否爲對象的一部分且是否可枚舉。 |
push 方法 | 將新元素追加到一個數組中,並返回數組的新長度。 |
reduce 方法 | 通過對數組中的所有元素調用定義的回調函數來累積單個結果。 回調函數的返回值是累積的結果,並且作爲對回調函數的下一個調用中的參數提供。 |
reduceRight 方法 | 通過對數組中的所有元素調用定義的回調函數來按降序順序累積單個結果。 回調函數的返回值是累積的結果,並且作爲對回調函數的下一個調用中的參數提供。 |
reverse 方法 | 將元素順序被反轉的 Array 對象返回。 |
shift 方法 | 從數組中移除第一個元素並將返回該元素。 |
slice 方法(數組) | 返回一個數組中的一部分。 |
some 方法 | 檢查定義的回調函數是否爲數組的任何元素返回 true。 |
sort 方法 | 返回一個元素已經進行了排序的 Array 對象。 |
splice 方法 | 從一個數組中移除元素,如有必要,在所移除元素的位置上插入新元素,並返回所移除的元素。 |
toLocaleString 方法 | 返回使用當前區域設置的字符串。 |
toString 方法 | 返回數組的字符串表示形式。 |
unshift 方法 | 在數組的開頭插入新元素。 |
valueOf 方法 | 獲取對數組的引用。 |
values 方法 | 返回包含數組的值的迭代器。 |
1、concat 方法
組合兩個或兩個以上的數組。
array1.concat([item1[, item2[, … [, itemN]]]])
參數 | 描述 |
---|---|
array1 | 必需。其他數組連接到的 Array 對象。 |
item1,…, itemN | 可選。要添加到 array1 的末尾的附加項。 |
var a, b, c, d;
a = new Array(1,2,3);
b = "dog";
c = new Array(42, "cat");
d = a.concat(b, c);
document.write(d);
//Output:
1, 2, 3, "dog", 42, "cat"
2、entries 方法
返回一個迭代器,它返回數組的鍵/值對。
arrayObj.entries();
參數 | 描述 |
---|---|
arrayObj | 必需。數組對象。 |
var entries = ["a", "b", "c"].entries();
// entries.next().value == [0, "a"]
// entries.next().value == [1, "b"]
// entries.next().value == [2, "c"]
3、every 方法
確定數組的所有成員是否滿足指定的測試。
array1.every(callbackfn[, thisArg])
參數 | 描述 |
---|---|
array1 | 必需。一個數組對象。 |
callbackfn | 必需。一個接受最多三個參數的函數。 every 方法會爲 array1 中的每個元素調用 callbackfn 函數,直到 callbackfn 返回 false,或直到到達數組的結尾。 |
thisArg | 可選。可在 callbackfn 函數中爲其引用 this 關鍵字的對象。如果省略 thisArg,則 undefined 將用作 this 值。 |
// Create a function that returns true if the value is
// numeric and within range.
var checkNumericRange = function(value) {
if (typeof value !== 'number')
return false;
else
return value >= this.minimum && value <= this.maximum;
}
// Create an array of numbers.
var numbers = [10, 15, 19];
// Check whether the callback function returns true for
// all of the array values.
// The obj argument enables use of the this value
// within the callback function.
var obj = { minimum: 10, maximum: 20 }
if (numbers.every(checkNumericRange, obj))
document.write ("All are within range.");
else
document.write ("Some are not within range.");
// Output:
// All are within range.
4、fill 方法
使用指定值填充數組。
arrayObj.fill(value [ , start [ , end ] ]);
參數 | 描述 |
---|---|
arrayObj | 必需。數組對象。 |
value | 必需。用於填充數組的值。 |
start | 可選。用於填充數組值的起始索引。默認值爲 0。 |
end | 可選。用於填充數組值的結束索引。默認值是 this 對象的 length 屬性。 |
備註:如果 start 爲負,則 start 被視爲 length+start,即-1爲最後一位,其中,length 是數組的長度。如果 end 爲負,則 end 被視爲 length+end。
[0, 0, 0].fill(7, 1);
// Array contains [0,7,7]
[0, 0, 0].fill(7);
// Array contains [7,7,7]
5、filter 方法
返回數組中的滿足回調函數中指定的條件的元素。
array1.filter(callbackfn[, thisArg])
參數 | 描述 |
---|---|
array1 | 必需。一個數組對象。 |
callbackfn | 必需。一個接受最多三個參數的函數。對於數組中的每個元素,filter 方法都會調用 callbackfn 函數一次。 |
thisArg | 可選。可在 callbackfn 函數中爲其引用 this 關鍵字的對象。如果省略 thisArg,則 undefined 將用作 this 值 |
回調函數
function callbackfn(value, index, array1)
參數 | 描述 |
---|---|
Value | 數組元素的值。 |
index | 數組元素的數字索引。 |
array1 | 包含該元素的數組對象。 |
var checkNumericRange = function(value) {
if (typeof value !== 'number')
return false;
else
return value >= this.minimum && value <= this.maximum;
}
var numbers = [6, 12, "15", 16, "the", -12];
// The obj argument enables use of the this value
// within the callback function.
var obj = { minimum: 10, maximum: 20 }
var result = numbers.filter(checkNumericRange, obj);
document.write(result);
// Output: 12,16
6、findIndex 方法
返回滿足回調函數中指定的測試條件的第一個數組元素的索引值。
arrayObj.findIndex(callbackfn [, thisArg]);
參數 | 描述 |
---|---|
arrayObj | 必需。數組對象。 |
callbackfn | 必需。用於測試數組中的每個元素的回調函數。 |
thisArg | 可選。指定回調函數中的 this 對象。如果未指定,則未定義 this 對象。 |
備註:對於數組中的每個元素,findIndex 方法都會調用一次回調函數(採用升序索引順序),直到有元素返回 true。只要有一個元素返回 true,findIndex 立即返回該返回 true 的元素的索引值。如果數組中沒有任何元素返回 true,則 findIndex 返回 -1。
回調函數
function callbackfn(value, index, thisArg)
參數 | 描述 |
---|---|
Value | 數組元素的值。 |
index | 數組元素的數字索引。 |
arrayObj | 要遍歷的數組對象。 |
[1,2,3].findIndex(function(x) { x == 2; });
// Returns an index value of 1.
7、forEach 方法
爲數組中的每個元素執行指定操作。
array1.forEach(callbackfn[, thisArg])
參數 | 描述 |
---|---|
array1 | 必選。一個數組對象。 |
callbackfn | 必選。最多可以接受三個參數的函數。對於數組中的每個元素,forEach 都會調用 callbackfn 函數一次。 |
thisArg | 可選。 callbackfn 函數中的 this 關鍵字可引用的對象。如果省略 thisArg,則 undefined 將用作 this 值。 |
回調函數
function callbackfn(value, index, array1)
參數 | 描述 |
---|---|
Value | 數組元素的值。 |
index | 數組元素的數字索引。 |
array1 | 包含該元素的數組對象。 |
// Define the object that contains the callback function.
var obj = {
showResults: function(value, index) {
// Call calcSquare by using the this value.
var squared = this.calcSquare(value);
document.write("value: " + value);
document.write(" index: " + index);
document.write(" squared: " + squared);
document.write("<br />");
},
calcSquare: function(x) { return x * x }
};
// Define an array.
var numbers = [5, 6];
// Call the showResults callback function for each array element.
// The obj is the this value within the
// callback function.
numbers.forEach(obj.showResults, obj);
// Embed the callback function in the forEach statement.
// The obj argument is the this value within the obj object.
// The output is the same as for the previous statement.
numbers.forEach(function(value, index) { this.showResults(value, index) }, obj);
// Output:
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
// value: 5 index: 0 squared: 25
// value: 6 index: 1 squared: 36
8、indexOf 方法
返回某個值在數組中的第一個匹配項的索引。
array1.indexOf(searchElement[, fromIndex])
參數 | 描述 |
---|---|
array1 | 必需。一個數組對象。 |
searchElement | 必需。要在 array1 中定位的值。 |
fromIndex | 可選。用於開始搜索的數組索引。如果省略 fromIndex,則從索引 0 處開始搜索。 |
// Create an array. (The elements start at index 0.)
var ar = ["ab", "cd", "ef", "ab", "cd"];
// Determine the first location of "cd".
document.write(ar.indexOf("cd") + "<br/>");
// Output: 1
// Find "cd" starting at index 2.
document.write(ar.indexOf("cd", 2) + "<br/>");
// Output: 4
// Find "gh" (which is not found).
document.write (ar.indexOf("gh")+ "<br/>");
// Output: -1
// Find "ab" with a fromIndex argument of -2.
// The search starts at index 3, which is the array length plus -2.
document.write (ar.indexOf("ab", -2) + "<br/>");
// Output: 3
9、join 方法
添加由指定分隔符字符串分隔的數組的所有元素
arrayObj.join([separator])
參數 | 描述 |
---|---|
arrayObj | 必需。 一個 Array 對象。 |
separator | 可選。 用於將在結果 String 中的數組的一個元素與下一個元素分開的字符串。 若忽略此參數,則數組元素之間用逗號分隔。 |
備註:如果數組的任一元素爲 undefined 或 null,則該元素將被視爲空字符串。
var a, b;
a = new Array(0,1,2,3,4);
b = a.join("-");
document.write(b);
// Output:
// 0-1-2-3-4
10、keys 方法
返回一個迭代器,它能返回數組的索引值。
arrayObj.keys();
var k = [“a”, “b”, “c”].keys();
// k.next().value == 0
// k.next().value == 1
// k.next().value == 2
11、lastIndexOf 方法
返回指定的值在數組中的最後一個匹配項的索引。
array1.lastIndexOf(searchElement[, fromIndex])
參數 | 描述 |
---|---|
array1 | 必需。要搜索的數組對象。 |
searchElement | 必需。要在 array1 中定位的值。 |
fromIndex | 可選。用於開始搜索的數組索引。如果省略 fromIndex,則搜索將從數組中的最後一個索引處開始。 |
// Create an array.
var ar = ["ab", "cd", "ef", "ab", "cd"];
// Determine the first location, in descending order, of "cd".
document.write(ar.lastIndexOf("cd") + "<br/>");
// Output: 4
// Find "cd" in descending order, starting at index 2.
document.write(ar.lastIndexOf("cd", 2) + "<br/>");
// Output: 1
// Search for "gh" (which is not found).
document.write(ar.lastIndexOf("gh")+ "<br/>");
// Output: -1
// Find "ab" with a fromIndex argument of -3.
// The search in descending order starts at index 3,
// which is the array length minus 2.
document.write(ar.lastIndexOf("ab", -3) + "<br/>");
// Output: 0
11、map 方法
對數組的每個元素調用定義的回調函數並返回包含結果的數組。
array1.map(callbackfn[, thisArg])
參數 | 描述 |
---|---|
array1 | 必選。 一個數組對象。 |
callbackfn | 必選。 最多可以接受三個參數的函數。 對於數組中的每個元素,map 方法都會調用 callbackfn 函數一次。 |
thisArg | 可選。 callbackfn 函數中的 this 關鍵字可引用的對象。 如果省略 thisArg,則 undefined 將用作 this 值。 |
// Define an object that contains a divisor property and
// a remainder function.
var obj = {
divisor: 10,
remainder: function (value) {
return value % this.divisor;
}
}
// Create an array.
var numbers = [6, 12, 25, 30];
// Get the remainders.
// The obj argument specifies the this value in the callback function.
var result = numbers.map(obj.remainder, obj);
document.write(result);
// Output:
// 6,2,5,0
12、pop 方法
從數組中移除最後一個元素並返回該元素。
arrayObj.pop( )
var number;
var my_array = new Array();
my_array.push (5, 6, 7);
my_array.push (8, 9);
number = my_array.pop();
while (number != undefined)
{
document.write (number + " ");
number = my_array.pop();
}
// Output: 9 8 7 6 5
13、push 方法
將新元素追加到一個數組中,並返回新的數組長度。
arrayObj.push([item1 [item2 [… [itemN ]]]])
var number;
var my_array = new Array();
my_array.push(5, 6, 7);
my_array.push(8, 9);
console.log(my_array);
14、reduce 方法
對數組中的所有元素調用指定的回調函數。該回調函數的返回值爲累積結果,並且此返回值在下一次調用該回調函數時作爲參數提供。
參數 | 描述 |
---|---|
array1 | 必需。一個數組對象。 |
callbackfn | 必需。一個接受最多四個參數的函數。對於數組中的每個元素,reduce 方法都會調用 callbackfn 函數一次。 |
initialValue | 可選。如果指定 initialValue,則它將用作初始值來啓動累積。第一次調用 callbackfn 函數會將此值作爲參數而非數組值提供 |
回調函數語法
function callbackfn(previousValue, currentValue, currentIndex, array1)
參數 | 描述 |
---|---|
previousValue | 通過上一次調用回調函數獲得的值。如果向 reduce 方法提供 initialValue,則在首次調用函數時,previousValue 爲 initialValue。 |
currentValue | 當前數組元素的值。 |
currentIndex | 當前數組元素的數字索引。 |
array1 | 包含該元素的數組對象。 |
function Process(previousArray, currentValue) {
// If currentValue is between 1 and 10,
// append currentValue to the array.
var nextArray;
if (currentValue >= 1 && currentValue <= 10)
nextArray = previousArray.concat(currentValue);
else
nextArray = previousArray;
// If this is not the last call by the reduce method,
// the returned array is previousArray on the next call.
// If this is the last call by the reduce method, the
// returned array is the return value of the reduce method.
return nextArray;
}
// Create an array.
var numbers = [20, 1, -5, 6, 50, 3];
// Call the reduce method, starting with an initial empty array.
var emptyArray = new Array();
var resultArray = numbers.reduce(Process, emptyArray);
document.write("result array=" + resultArray);
// Output:
// result array=1,6,3
15、reduceRight 方法
按降序順序對數組中的所有元素調用指定的回調函數。該回調函數的返回值爲累積結果,並且此返回值在下一次調用該回調函數時作爲參數提供。
array1.reduceRight(callbackfn[, initialValue])
參數 | 描述 |
---|---|
array1 | 必需。一個數組對象。 |
callbackfn | 必需。一個接受最多四個參數的函數。對於數組中的每個元素,reduceRight 方法都會調用 callbackfn 函數一次。 |
initialValue | 可選。如果指定 initialValue,則它將用作初始值來啓動累積。第一次調用 callbackfn 函數會將此值作爲參數而非數組值提供。 |
回調函數
function callbackfn(previousValue, currentValue, currentIndex, array1)
參數 | 描述 |
---|---|
previousValue | 通過上一次調用回調函數獲得的值。如果向 reduceRight 方法提供 initialValue,則在首次調用函數時,previousValue 爲 initialValue。 |
currentValue | 當前數組元素的值。 |
currentIndex | 當前數組元素的數字索引。 |
array1 | 包含該元素的數組對象。 |
function Process2(previousArray, currentValue) {
// If currentValue is between 1 and 10,
// append currentValue to the array.
var nextArray;
if (currentValue >= 1 && currentValue <= 10)
nextArray = previousArray.concat(currentValue);
else
nextArray = previousArray;
// If this is not the last call by the reduceRight method,
// the returned array is previousArray on the next call.
// If this is the last call by the reduceRight method, the
// returned array is the return value of the reduceRight method.
return nextArray;
}
// Create an array.
var numbers = [20, 1, -5, 6, 50, 3];
// Call the reduceRight method, starting with an empty array.
var emptyArray = new Array();
var resultArray = numbers.reduceRight(Process2, emptyArray);
document.write("result array=" + resultArray);
// Output:
// result array=3,6,1
16、reverse 方法
反轉 Array 中的元素
arrayObj.reverse()
var arr = new Array(0,1,2,3,4);
var reverseArr = arr.reverse();
document.write(reverseArr);
// Output:
// 4,3,2,1,0
17、shift 方法
從數組中移除第一個元素並將返回該元素。
arrayObj.shift( )
var arr = new Array(10, 11, 12);
while (arr.length > 0)
{
var i = arr.shift();
document.write (i.toString() + " ");
}
// Output:
// 10 11 12
18、slice 方法
返回一個數組中的一部分。
arrayObj.slice(start, [end])
參數 | 描述 |
---|---|
arrayObj | 必需。一個 Array 對象。 |
start | 必需。 arrayObj 的指定部分的開頭。 |
end | 可選。 arrayObj 的指定部分的結尾。 |
var origArray = [3, 5, 7, 9];
var newArray = origArray. slice(0, -1);
document.write(origArray);
document.write("<br/>");
newArray = origArray. slice(-2);
document.write(newArray);
// Output:
// 3,5,7,9
// 7,9
19、sort 方法
對 Array 排序。
arrayobj.sort(sortFunction)
參數 | 描述 |
---|---|
arrayObj | 必需。任意 Array 對象。 |
sortFunction | 可選。用來確定元素順序的函數的名稱。如果省略 ASCII 字符順序,則將按升序對這些元素進行排序。 |
備註:如果所傳遞的第一個參數小於第二個參數,則返回負值。
如果兩個參數相等,則返回零。
如果第一個參數大於第二個參數,則返回正值。
var a = new Array(4, 11, 2, 10, 3, 1);
var b = a.sort();
document.write(b);
document.write("<br/>");
// This is ASCII character order.
// Output: 1,10,11,2,3,4)
// Sort the array elements with a function that compares array elements.
b = a.sort(CompareForSort);
document.write(b);
document.write("<br/>");
// Output: 1,2,3,4,10,11.
// Sorts array elements in ascending order numerically.
function CompareForSort(first, second)
{
if (first == second)
return 0;
if (first < second)
return -1;
else
return 1;
}
20、splice 方法
從一個數組中移除元素,如有必要,在所移除元素的位置上插入新元素,並返回所移除的元素。
arrayObj.splice(start, deleteCount, [item1[, item2[, … [,itemN]]]])
參數 | 描述 |
---|---|
arrayObj | 必需。一個 Array 對象。 |
start | 必需。數組中移除元素操作的起點,從 0 開始。 |
deleteCount | 必需。要移除的元素數。 |
item1, item2,…, itemN | 可選。插入數組中代替已移除元素的元素。 |
var arr = new Array("4", "11", "2", "10", "3", "1");
arr.splice(2, 2, "21", "31");
document.write(arr);
// Output: 4,11,21,31,3,1
21、unshift 方法
在數組的開頭插入新元素。
arrayObj.unshift([item1[, item2 [, … [, itemN]]]])
參數 | 描述 |
---|---|
arrayObj | 必需。一個 Array 對象。 |
item1, item2,…, itemN | 可選。用於插到 Array 開頭的元素。 |
var ar = new Array();
ar.unshift(10, 11);
ar.unshift(12, 13, 14);
document.write(ar.toString());
// Output: 12,13,14,10,11
21、values 方法
返回一個迭代器,它返回數組的值
arrayObj.values();
var v = ["a", "b", "c"].values();
// v.next().value == "a"
// v.next().value == "b"
// v.next().value == "c"