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"