Array.of()
method creates a new
Array
instance from a variable number of arguments, regardless of number or type of the arguments.
The difference between
Array.of()
和
Array
constructor is in the handling of integer arguments:
Array.of(7)
creates an array with a single element,
7
, whereas
Array(7)
creates an empty array with a
length
property of
7
(
注意:
this implies an array of
7
empty slots, not slots with actual
undefined
values).
Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // array of 7 empty slots Array(1, 2, 3); // [1, 2, 3]
Array.of(element0[, element1[, ...[, elementN]]])
element
N
Elements used to create the array.
A new
Array
实例。
This function is part of the ECMAScript 2015 standard.
For more information, see:
Running the following code before any other code will create
Array.of()
if it's not natively available.
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
// Or
let vals = [];
for(let prop in arguments){
vals.push(arguments[prop]);
}
return vals;
}
}
Array.of(1); // [1] Array.of(1, 2, 3); // [1, 2, 3] Array.of(undefined); // [undefined]
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array.of' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
of
|
Chrome 45 | Edge 12 | Firefox 25 | IE No | Opera 26 | Safari 9 | WebView Android 39 | Chrome Android 39 | Firefox Android 25 | Opera Android 26 | Safari iOS 9 | Samsung Internet Android 4.0 | nodejs 4.0.0 |
完整支持
不支持
Array
Array.from()
Array.isArray()
Array.of()
Array.prototype.concat()
Array.prototype.copyWithin()
Array.prototype.entries()
Array.prototype.every()
Array.prototype.fill()
Array.prototype.filter()
Array.prototype.find()
Array.prototype.findIndex()
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.forEach()
Array.prototype.includes()
Array.prototype.indexOf()
Array.prototype.join()
Array.prototype.keys()
Array.prototype.lastIndexOf()
Array.prototype.map()
Array.prototype.pop()
Array.prototype.push()
Array.prototype.reduce()
Array.prototype.reduceRight()
Array.prototype.reverse()
Array.prototype.shift()
Array.prototype.slice()
Array.prototype.some()
Array.prototype.sort()
Array.prototype.splice()
Array.prototype.toLocaleString()
Array.prototype.toSource()
Array.prototype.toString()
Array.prototype.unshift()
Array.prototype.values()
Array.prototype[@@iterator]()
get Array[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()