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']);
it.next().value; //'a'
it.next().value; //'b'
it.next().done; // true
function idMaker() {
var index = 0;
return {
next: function() {
return {value: index++, done: false};
}
}
}
var it = idMaker();
it.next().value; //'0'
it.next().value; //'1'
it.next().value; //'2'
function idMaker() {
var index = 0;
return {
next: function() {
return {value: index++, done: false};
}
}
}
for (var n of it) {
if (n > 5) {
break;
console.log( n );
}
}
//0
//1
//2
//3
//4
//5
const arr = [1, 5, 3, 9];
for (let v of arr) {
console.log( v );
}
//1
//5
//3
//9
var arr = ['a', 'b', 'c', 'd'];
for (a in arr) {
console.log( a );
}
//0
//1
//2
//3
for (a of arr) {
console.log( a );
}
//0
//1
//2
//3
var name = ['S', 'D', 'J', 'Z', 'G', 'G', 'G'];
for ( var e of name) {
console.log( e );
}
//S
//D
//J
//Z
//G
var es6 = new Map();
es6.set('edition', 6);
es6.set('committee', 'TC39');
es6.set('standard', 'ECMA-262');
for(var [name, value] of es6) {
console.log(name + ": " + value);
}
// edition: 6
// commttee: TC39
// standard: ECMA-262
var es6 = {
name: "G.Dragon",
year: 22,
love: "coding"
};
for (e in es6) {
console.log( e );
}
//name
//year
//love
for( e of es6) {
console.log( e );
}
// TypeError: es6 is not iterable
// 字符串例子
let str = "hello";
for (let s of str) {
console.log( s );
}
//h
//e
//l
//l
//o
// DOM NodeList对象的例子
let paras = document.getSelectorAll("p");
for (let p of paras) {
p.classList.add("test");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有