JS——for...of...

来源:互联网 发布:ubuntu ftp传输文件 编辑:程序博客网 时间:2024/06/16 01:22
//The for...of statement creates a loop iterating over iterable objects (including Array, Map, Set, String, TypedArray, arguments object and so on), invoking a custom iteration hook with statements to be executed for the value of each distinct property.//循环遍历可以遍历的对象,包括(Array, Map, Set, String, TypedArray, arguments object等等)var arr=[5,6,7,8,9];for(var item of arr){console.log(item);}var str='abcdefg';for(var item of arr){console.log(item);}//不能遍历对象/*var per={name:"abc",age:20}for(var item of per){console.log(item);  //报错}*//* ES6 借鉴 C++、Java、C# 和 Python 语言,引入了for...of循环,作为遍历所有数据结构的统一的方法。一个数据结构只要部署了Symbol.iterator属性,就被视为具有 iterator 接口,就可以用for...of循环遍历它的成员。也就是说,for...of循环内部调用的是数据结构的Symbol.iterator方法。for...of循环可以使用的范围包括数组、Set 和 Map 结构、某些类似数组的对象(比如arguments对象、DOM NodeList 对象)、后文的 Generator 对象,以及字符串。 */

原创粉丝点击