Array.of() 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。
Array.of() 和 Array 构造函数之间的区别在于处理整数参数:Array.of(7) 创建一个具有单个元素 7 的数组,而 Array(7) 创建一个长度为7的空数组(注意:这是指一个有7个空位(empty)的数组,而不是由7个undefined组成的数组)。
Array.of(7); // [7] Array.of(1, 2, 3); // [1, 2, 3] Array(7); // [ , , , , , , ] Array(1, 2, 3); // [1, 2, 3]
语法
Array.of(element0[, element1[, ...[, elementN]]])
参数
- element N
- 任意个参数,将按顺序成为返回数组中的元素。
返回值
新的 Array 实例。
描述
此函数是ECMAScript 2015标准的一部分。详见 Array.of 和 Array.from proposal 和 Array.of polyfill。
示例
Array.of(1); // [1] Array.of(1, 2, 3); // [1, 2, 3] Array.of(undefined); // [undefined]
兼容旧环境
如果原生不支持的话,在其他代码之前执行以下代码会创建 Array.of() 。
if (!Array.of) {
Array.of = function() {
return Array.prototype.slice.call(arguments);
};
}
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Array.of |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) Array.of |
Draft |
浏览器兼容性
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
of |
Chrome Full support 45 | Edge Full support 12 | Firefox Full support 25 | IE No support No | Opera Full support Yes | Safari Full support 9 | WebView Android Full support Yes | Chrome Android Full support 39 | Firefox Android Full support 25 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support 4.0 | nodejs Full support 4.0.0 |
Legend
- Full support
- Full support
- No support
- No support