aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/array/of/index.html
blob: 4636c06bdb275ad09775f5d42d4e5d72447b6a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
title: Array.of()
slug: Web/JavaScript/Reference/Global_Objects/Array/of
tags:
  - Array
  - Array.of()
  - ECMAScript 2015
  - ES 6
  - JavaScript
  - polyfill
  - 方法
translation_of: Web/JavaScript/Reference/Global_Objects/Array/of
---
<div>{{JSRef}}</div>

<p><code><strong>Array.of()</strong></code> 方法创建一个具有可变数量参数的新数组实例,而不考虑参数的数量或类型。</p>

<p> <code><strong>Array.of()</strong></code> 和 <code><strong>Array</strong></code> 构造函数之间的区别在于处理整数参数:<code><strong>Array.of(7)</strong></code><strong> </strong>创建一个具有单个元素 <strong>7</strong> 的数组,而 <strong><code>Array(7)</code> </strong>创建一个长度为7的空数组(<strong>注意:</strong>这是指一个有7个空位(empty)的数组,而不是由7个<code>undefined</code>组成的数组)。</p>

<pre class="brush: js">Array.of(7);       // [7]
Array.of(1, 2, 3); // [1, 2, 3]

Array(7);          // [ , , , , , , ]
Array(1, 2, 3);    // [1, 2, 3]
</pre>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox"><code>Array.of(<var>element0</var>[, <var>element1</var>[, ...[, <var>elementN</var>]]])</code></pre>

<h3 id="Parameters" name="Parameters">参数</h3>

<dl>
 <dt>element<em>N</em></dt>
 <dd>任意个参数,将按顺序成为返回数组中的元素。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>新的 {{jsxref("Array")}} 实例。</p>

<h2 id="描述">描述</h2>

<p>此函数是ECMAScript 2015标准的一部分。详见 <a href="https://gist.github.com/rwaldron/1074126"><code>Array.of 和</code> <code>Array.from</code> proposal</a> 和 <a href="https://gist.github.com/rwaldron/3186576"><code>Array.of</code> polyfill</a></p>

<h2 id="示例">示例</h2>

<pre class="brush: js" style="font-size: 14px;"><span style="line-height: 22px;">Array.of(1);         // [1]
</span><span style="line-height: 22px;">Array.of(1, 2, 3);   // [1, 2, 3]</span>
Array.of(undefined); // [undefined]
</pre>

<h2 id="Compatibility" name="Compatibility">兼容旧环境</h2>

<p>如果原生不支持的话,在其他代码之前执行以下代码会创建 <code>Array.of()</code> 。</p>

<pre class="brush: js" style="font-size: 14px;">if (!Array.of) {
  Array.of = function() {
    return Array.prototype.slice.call(arguments);
  };
}</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-array.of', 'Array.of')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.of', 'Array.of')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>


<p>{{Compat("javascript.builtins.Array.of")}}</p>
</div>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("Array")}}</li>
 <li>{{jsxref("Array.from()")}}</li>
 <li>{{jsxref("TypedArray.of()")}}</li>
</ul>