diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
| commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
| tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/vi/web/javascript/reference/statements/for...of | |
| parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
| download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip | |
initial commit
Diffstat (limited to 'files/vi/web/javascript/reference/statements/for...of')
| -rw-r--r-- | files/vi/web/javascript/reference/statements/for...of/index.html | 318 |
1 files changed, 318 insertions, 0 deletions
diff --git a/files/vi/web/javascript/reference/statements/for...of/index.html b/files/vi/web/javascript/reference/statements/for...of/index.html new file mode 100644 index 0000000000..5bd72040eb --- /dev/null +++ b/files/vi/web/javascript/reference/statements/for...of/index.html @@ -0,0 +1,318 @@ +--- +title: for...of +slug: Web/JavaScript/Reference/Statements/for...of +tags: + - ECMAScript 2015 + - JavaScript + - Reference + - Statement +translation_of: Web/JavaScript/Reference/Statements/for...of +--- +<div>{{jsSidebar("Statements")}}</div> + +<p>Cú pháp <strong><code>for...of</code></strong> để chạy <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">vòng lặp </a> trên {{jsxref("String")}}, {{jsxref("Array")}}, đối tượng tương tự <code>Array</code> (như {{jsxref("Functions/arguments", "arguments")}} hoặc {{domxref("NodeList")}}), {{jsxref("TypedArray")}}, {{jsxref("Map")}}, {{jsxref("Set")}}.</p> + +<div>{{EmbedInteractiveExample("pages/js/statement-forof.html")}}</div> + +<p class="hidden">Ví dụ được lưu trên GitHub repository. Nếu muốn đóng góp, bạn có thể clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> và gởi lên pull request.</p> + +<h2 id="Cú_pháp">Cú pháp</h2> + +<pre class="syntaxbox notranslate">for (<em>tên-biến</em> of <em>đối-tượng-chạy-vòng-lặp</em>) { + <em>...câu lệnh...</em> +} +</pre> + +<dl> + <dt><code>tên biến</code></dt> + <dd><br> + Trên mỗi lần lặp, một giá trị của một thuộc tính khác nhau được gán cho biến. biến có thể được khai báo với const, let hoặc var.</dd> + <dt><code>đối tượng để chạy vòng lặp</code></dt> + <dd>Đối tượng có các thuộc tính có thể được lặp lại (<em>iterable</em>).</dd> +</dl> + +<h2 id="Ví_dụ">Ví dụ</h2> + +<h3 id="Lặp_qua_một_jsxrefArray">Lặp qua một {{jsxref("Array")}}</h3> + +<pre class="brush:js notranslate">let iterable = [10, 20, 30]; + +for (let value of iterable ) { + value += 1; + console.log(value); +} +// 11 +// 21 +// 31 +</pre> + +<p>Có thể khai báo bằng <a href="/en-US/docs/Web/JavaScript/Reference/Statements/const"><code>const</code></a> thay cho <a href="/en-US/docs/Web/JavaScript/Reference/Statements/let"><code>let</code></a>, nếu không có thay đổi biến bên trong vòng lặp.</p> + +<pre class="brush:js notranslate">let iterable= [10, 20, 30]; + +for (const value of iterable) { + console.log(value); +} +// 10 +// 20 +// 30 +</pre> + +<h3 id="Lặp_qua_một_jsxrefString">Lặp qua một {{jsxref("String")}}</h3> + +<pre class="brush:js notranslate">const iterable = 'boo'; + +for (const value of iterable) { + console.log(value); +} +// "b" +// "o" +// "o" +</pre> + +<h3 id="Lặp_qua_jsxrefTypedArray">Lặp qua {{jsxref("TypedArray")}}</h3> + +<pre class="brush:js notranslate">const iterable = new Uint8Array([0x00, 0xff]); + +for (const value of iterable) { + console.log(value); +} +// 0 +// 255 +</pre> + +<h3 id="Lặp_qua_một_jsxrefMap">Lặp qua một {{jsxref("Map")}}</h3> + +<pre class="brush:js notranslate">const iterable = new Map([['a', 1], ['b', 2], ['c', 3]]); + +for (const entry of iterable) { + console.log(entry); +} +// ['a', 1] +// ['b', 2] +// ['c', 3] + +for (const [key, value] of iterable) { + console.log(value); +} +// 1 +// 2 +// 3 +</pre> + +<h3 id="Loop_qua_một_jsxrefSet">Loop qua một {{jsxref("Set")}}</h3> + +<pre class="brush:js notranslate">const iterable = new Set([1, 1, 2, 2, 3, 3]); + +for (const value of iterable) { + console.log(value); +} +// 1 +// 2 +// 3 +</pre> + +<h3 id="Lặp_qua_một_đối_tượng_arguments">Lặp qua một đối tượng <code>arguments</code></h3> + +<p>Lặp qua đối tượng {{jsxref("Functions/arguments", "arguments")}} để có tất cả giá trị được truyền vào trong hàm:</p> + +<pre class="brush: js notranslate">(function() { + for (const argument of arguments) { + console.log(argument); + } +})(1, 2, 3); + +// 1 +// 2 +// 3</pre> + +<h3 id="Lặp_qua_một_tập_DOM">Lặp qua một tập DOM</h3> + +<p>Lặp qua một tập DOM như {{domxref("NodeList")}}: ví dụ bên dưới, thêm class <code>read</code> cho các đoạn văn bản nào là con trực tiếp của article:</p> + +<pre class="brush:js notranslate">// Lưu ý: Chỉ hoạt động động trên các platforms có +// hiện thực NodeList.prototype[Symbol.iterator] +const articleParagraphs = document.querySelectorAll('article > p'); + +for (const paragraph of articleParagraphs) { + paragraph.classList.add('read'); +} +</pre> + +<h3 id="Đóng_vòng_lặp">Đóng vòng lặp</h3> + +<p>Trong vòng lặp <code>for...of</code>, có thể ngừng lặp giữa chừng bằng <code>break</code>, <code>continue</code>, <code>throw</code> hoặc <code>return</code>. Trong các trường hợp này, vòng lặp sẽ được ngưng lại.</p> + +<pre class="brush: js notranslate">function* foo(){ + yield 1; + yield 2; + yield 3; +}; + +for (const o of foo()) { + console.log(o); + break; // đóng vòng lặp, tiếp tục thực thi bên ngoài vòng lặp +} +console.log('Xong') +</pre> + +<h3 id="Lặp_qua_generator">Lặp qua generator</h3> + +<p>Bạn cũng có thể lặp qua hàm <a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">generators</a>, ví dụ:</p> + +<pre class="brush:js notranslate">function* fibonacci() { // một hàm generator + let [prev, curr] = [0, 1]; + while (true) { + [prev, curr] = [curr, prev + curr]; + yield curr; + } +} + +for (const n of fibonacci()) { + console.log(n); + // truncate the sequence at 1000 + if (n >= 1000) { + break; + } +} +</pre> + +<h4 id="Không_tái_sử_dụng_generator">Không tái sử dụng generator</h4> + +<p>Không nên re-used Generator, ngay cả khi vòng lặp <code>for...of</code> bị kết thúc sớm bằng {{jsxref("Statements/break", "break")}}. Khi thoát khỏi vòng lặp, generator sẽ kết thúc và cố lặp lại lần nữa sẽ không cho thêm bất kỳ kết quả yield nào khác.</p> + +<pre class="brush: js example-bad notranslate">const gen = (function *(){ + yield 1; + yield 2; + yield 3; +})(); +for (const o of gen) { + console.log(o); + break; // Closes iterator +} + +// Không dùng lại generator, đoạn code như thế này không hợp lý! +for (const o of gen) { + console.log(o); // Không bao giờ được gọi +} +</pre> + +<h3 id="Lặp_qua_các_đối_tượng_khác">Lặp qua các đối tượng khác</h3> + +<p>Bạn cũng có thể loop qua các đối tượng tự định nghĩa, nếu có hiện thực <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#iterable">iterable</a>:</p> + +<pre class="brush:js notranslate">const iterable = { + [Symbol.iterator]() { + return { + i: 0, + next() { + if (this.i < 3) { + return { value: this.i++, done: false }; + } + return { value: undefined, done: true }; + } + }; + } +}; + +for (const value of iterable) { + console.log(value); +} +// 0 +// 1 +// 2 +</pre> + +<h3 id="Sự_khác_biệt_giữa_for...of_và_for...in">Sự khác biệt giữa <code>for...of</code> và <code>for...in</code></h3> + +<p>Cú pháp {{jsxref("Statements/for...in", "for...in")}} lặp qua các đối tượng <a href="/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">được đếm</a>, theo một thứ tự tùy ý.</p> + +<p>Cú pháp <code>for...of</code> lặp qua đối tượng dữ liệu <a href="/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables">có thể lặp</a>.</p> + +<p>Ví dụ sau để thấy sự khác nhau giữa <code>for...of</code> và <code>for...in</code> khi sử dụng với {{jsxref("Array")}}.</p> + +<pre class="brush:js notranslate">Object.prototype.objCustom = function() {}; +Array.prototype.arrCustom = function() {}; + +const iterable = [3, 5, 7]; +iterable.foo = 'hello'; + +for (const i in iterable) { + console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom" +} + +for (const i in iterable) { + if (iterable.hasOwnProperty(i)) { + console.log(i); // logs 0, 1, 2, "foo" + } +} + +for (const i of iterable) { + console.log(i); // logs 3, 5, 7 +} +</pre> + +<p>Giải thích ví dụ trên</p> + +<pre class="brush: js notranslate">Object.prototype.objCustom = function() {}; +Array.prototype.arrCustom = function() {}; + +const iterable = [3, 5, 7]; +iterable.foo = 'hello';</pre> + +<p>Tất cả object sẽ kế thừa thuộc tính <code>objCustom</code> và tất cả {{jsxref("Array")}} sẽ kết thừa thuộc tính <code>arrCustom</code> bởi vì chúng ta thêm nó vào bằng {{jsxref("Object.prototype")}} và {{jsxref("Array.prototype")}}. <code>iterable</code> kế thừa cả <code>objCustom</code> và <code>arrCustom</code>.</p> + +<pre class="brush: js notranslate">for (const i in iterable) { + console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom" +}</pre> + +<p>Vòng vòng lặp này chỉ log <a href="/en-US/docs/Web/JavaScript/Enumerability_and_ownership_of_properties">thuộc tính được đếm</a> của <code>iterable</code>, theo thứ tự được đưa vào. Nó không log các <strong>element</strong> của array <code>3</code>, <code>5</code>, <code>7</code> hoặc <code>hello</code> bởi vì nó là không thuộc tính được đếm. Nó log giá trị <strong>index</strong> cũng như <code>arrCustom</code> và <code>objCustom</code>.</p> + +<pre class="brush: js notranslate">for (let i in iterable) { + if (iterable.hasOwnProperty(i)) { + console.log(i); // logs 0, 1, 2, "foo" + } +}</pre> + +<p>Vòng loop tương tự như ở trên, nhưng sử dụng {{jsxref("Object.prototype.hasOwnProperty()", "hasOwnProperty()")}} để kiểm tra, nếu tìm thấy một property của chính nó chứ không phải kế thừa và log kết quả ra. Các Property <code>0</code>, <code>1</code>, <code>2</code> và <code>foo</code> được log bởi vì nó không phải được kết thừa.</p> + +<pre class="brush: js notranslate">for (const i of iterable) { + console.log(i); // logs 3, 5, 7 +}</pre> + +<p>Vòng lặp và log ra giá trị bên trong đối tượng <code>iterable</code> như một <a href="/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators#Iterables">iterable object</a><strong> </strong> được khai báo để lặp, chính là các element bên trong mảng <code>3</code>, <code>5</code>, <code>7</code> và không bao gồm các <strong>property</strong> của object.</p> + +<h2 id="Đặc_điểm">Đặc điểm</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Đặc điểm</th> + <th scope="col">Status</th> + <th scope="col">Ghi chú</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-for-in-and-for-of-statements', 'for...of statement')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-for-in-and-for-of-statements', 'for...of statement')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Trình_duyệt_hổ_trợ">Trình duyệt hổ trợ</h2> + +<div class="hidden">Nếu muốn đóng góp dữ liệu cho bảng này, vui lòng check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> và gởi chúng tôi pull request.</div> + +<p>{{Compat("javascript.statements.for_of")}}</p> + +<h2 id="Xem_thêm">Xem thêm</h2> + +<ul> + <li>{{jsxref("Array.prototype.forEach()")}}</li> + <li>{{jsxref("Map.prototype.forEach()")}}</li> +</ul> |
