Array()
constructor is used to create
Array
对象。
[element0, element1, ..., elementN] new Array(element0, element1[, ...[, elementN]]) new Array(arrayLength)
element
N
Array
constructor and that argument is a number (see the arrayLength parameter below). Note that this special case only applies to JavaScript arrays created with the
Array
constructor, not array literals created with the bracket syntax.
arrayLength
Array
constructor is an integer between 0 and 2
32
-1 (inclusive), this returns a new JavaScript array with its
length
property set to that number (
注意:
this implies an array of
arrayLength
empty slots, not slots with actual
undefined
values). If the argument is any other number, a
RangeError
exception is thrown.
Arrays can be created using the literal 表示法:
let fruits = ['Apple', 'Banana']; console.log(fruits.length); // 2 console.log(fruits[0]); // "Apple"
Arrays can be created using a constructor with a single number parameter. An array with its
length
property set to that number and the array elements are empty slots.
let fruits = new Array(2); console.log(fruits.length); // 2 console.log(fruits[0]); // undefined
If more than one argument is passed to the constructor, a new
Array
with the given elements is created.
let fruits = new Array('Apple', 'Banana');
console.log(fruits.length); // 2
console.log(fruits[0]); // "Apple"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Array constructor' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Array()
构造函数
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 4 | Safari 1 | WebView Android ≤37 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持
Array
class
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()