{
let arr = ['hello', 'world'];
let map1 = arr[Symbol.iterator]();
// console.log(map1.next());
// console.log(map1.next());
} {
let obj = {
start: [1, 2, 3],
end: [7, 8, 9],
[Symbol.iterator]() {
let self = this;
let index = 0;
let arr = self.start.concat(self.end);
let len = arr.length;
return {
next() {
if (index < len) {
return {
value: arr[index++],
done: false
}
} else {
return {
value: arr[index++],
done: true
}
}
}
}
}
}
for (let key of obj) {
// console.log(key);
}
} {
var it = makeIterator(['a', 'b']);
// console.log(it.next()); // { value: "a", done: false }
// console.log(it.next()); // { value: "b", done: false }
function makeIterator(array) {
var nextIndex = 0;
return {
next: function() {
return nextIndex < array.length ? {
value: array[nextIndex++],
done: false
}: {
value: undefined,
done: true
};
}
};
}
} {
let generator = function * () {
yield 1;
yield * [2, 3, 4];
yield 5;
};
var iterator = generator();
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
} {
var str = new String("hi");
console.log([...str]);
str[Symbol.iterator] = function() {
return {
next: function() {
if (this._first) {
this._first = false;
return {
value: "bye",
done: false
};
} else {
return {
done: true
};
}
},
_first: true
};
};
console.log([...str]);
console.log(str);
}
console