Ecmascript6--iterable

Source: Internet
Author: User
Tags iterable new set

The ES6 Standard introduces new iterable types, Array Map and all of Set them belong to iterable types. a collection of 1.for of loops with a iterable type can be traversed by a new for ... of loop. var a = [' A ', ' B ', ' C '];
A.name = ' Hello ';
for (var x of a) {
alert (x); ' A ', ' B ', ' C '
} for ... of for ... in What is the difference between loops and loops ?   The for ... in loop is actually a property name for an object because of a history legacy problem. An array is actually an object, and its index of each element is treated as an attribute (for in loops include name, but the length property of the array is not included;)andFor : of loops He only loops the collection itself, returning the key-value pairs in the array instead of the attributes. var a = [' A ', ' B ', ' C '];a.name = ' Hello ';
for (Var x in a) {
alert (x); ' 0 ', ' 1 ', ' 2 ', ' name '
} However, a better approach is to use the built-in iterable forEach method directly, which receives a function that automatically callbacks the function each time it iterates. Take Array for example: var a = [' A ', ' B ', ' C '];
A.foreach (function (element, index, array) {
Element: A value that points to the current element
Index: point to Current index
Array: points to the array object itself
alert (element);
}); Set Array Similar but Set no index, so the first two parameters of the callback function are the elements themselves: var s = new Set ([' A ', ' B ', ' C ']);
S.foreach (function (element, sameelement, set) {
alert (element);
}); Map The callback function parameters are sequentially value , key and map themselves: var a = [' A ', ' B ', ' C '];
A.foreach (function (Element) {
alert (element);
});If you are not interested in certain parameters, they can be ignored because JavaScript function calls do not require parameters to be consistent. For example, you only need to obtain Array element : var a = [' A ', ' B ', ' C '];
A.foreach (function (Element) {
alert (element);
});

Ecmascript6--iterable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.