aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/global_objects/array/of/index.html
blob: fdd6d03d53e1ef40230f94356dd2fcd11432e972 (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
---
title: Array.of()
slug: Web/JavaScript/Reference/Global_Objects/Array/of
translation_of: Web/JavaScript/Reference/Global_Objects/Array/of
---
<div>{{JSRef}}</div>

<p>Phương thức <code><strong>Array.of()</strong></code> được dùng để tạo ra một mảng mới với các phần tử được truyền vào thông qua tham số truyền vào.</p>

<p>Cả <code><strong>Array.of()</strong></code><code><strong>Array </strong></code>đều chấp nhận tham số đầu vào là các số nguyên để khởi tạo một mảng mới, tuy nhiên chúng ta cần lưu ý trong trường hợp chỉ truyền vào một giá trị nguyên,  <code><strong>Array.of()</strong></code> sẽ tạo ra một mảng số nguyên có một phần tử duy nhất, trong khi <code><strong>Array()</strong></code> ?sẽ tạo ra một mảng có số lượng phần tử rỗng (phần tử rỗng không mang giá trị undefined hoặc null)  tương ứng với giá trị số nguyên truyền vào.</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="Cú_pháp"> Cú pháp</h2>

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

<h3 id="Tham_số">Tham số</h3>

<dl>
 <dt><code>element<em>N</em></code></dt>
 <dd>Các phần tử dùng để khởi tạo mảng</dd>
</dl>

<h3 id="Giá_trị_trả_về">Giá trị trả về</h3>

<p>Kết quả sau khi thực thi sẽ trả về một mảng mới</p>

<h2 id="Mô_tả">Mô tả</h2>

<p>Đây là một phương thức chuẩncủa ECMAScript 2015. Có thể tham khảo thêm <a href="https://gist.github.com/rwaldron/1074126"><code>Array.of</code> and <code>Array.from</code> proposal</a> và <a href="https://gist.github.com/rwaldron/3186576"><code>Array.of</code> polyfill</a>.</p>

<h2 id="Ví_dụ">Ví dụ</h2>

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

<h2 id="Polyfill">Polyfill</h2>

<p>Nếu trình duyệt không hỗ trợ <code>Array.of()</code>, có thể gắn đoạn mã JS sau và thực thi nó trước tất cả các đoạn mã JS khác.</p>

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

<h2 id="Đặc_tả">Đặc tả</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Đặc tả</th>
   <th scope="col">Trạng thái</th>
   <th scope="col">Ghi chú</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-array.of', 'Array.of')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Định nghĩa lần đầu.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.of', 'Array.of')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Tính_tương_thích_giữa_các_trình_duyệt">Tính tương thích giữa các trình duyệt</h2>

<div>


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

<h2 id="Tham_khảo_thêm">Tham khảo thêm</h2>

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