var arr = [1,2,3,4,5,6];
arr.name = 'a';
for (var index = 0; index < arr.length; index++) {
console.log(arr[index]);
}
arr.forEach(function (value) { //ES5 内建的forEach方法 缺陷:无法使用break 中断 ,也不能使用return 语句返回到外层函数
console.log(value);
});
for (var index in arr) { // 千万别这样做
console.log(arr[index]);
}
var b = 0;
for (var index in arr) {
b = b+ index;
console.log(b)
}
for(var value of arr){
console.log(value)
}
var words = 'a';
var s = new Set();
s.add("a");
s.add(1);
for(var word of s){
console.log(word);
}
var map = new Map();
map.set('a',1);
map.set('b',2);
map.set('c',3);
map.set('d',4);
for(var [key,value] of map){
console.log(key+':'+value);
}
//模拟遍历器原理
function makeIterator(array){
var nextIndex = 0;
return {
next: function(){
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{value: undefined, done: true};
}
}
}
var it = makeIterator(['a', 'b']);
console.log(it.next());//{ value: 'a', done: false }
console.log(it.next());//{ value: 'b', done: false }
console.log(it.next());//{ value: undefined, done: true }
var map = new Map(); console.log(map[Symbol.iterator] === map.entries)//true var arr = new Array(); console.log(arr[Symbol.iterator] === arr.values)//true var set = new Set(); console.log(set[Symbol.iterator] === set.values)//true
var students = {}
students[Symbol.iterator] = function() {
let index = 1;
return {
next() {
return {done: index>10, value: index++}
}
}
}
for(var i of students) {
console.log(i);
}//
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有