-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiterator.js
More file actions
35 lines (32 loc) · 822 Bytes
/
iterator.js
File metadata and controls
35 lines (32 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const obj = {
key1: 1,
key2: 2,
key3: 3,
};
Object.defineProperty(obj, Symbol.iterator, {
enumerable: false,
writable: false,
configurable: true,
value: function() {
const ref = this;
console.log(ref.length);
const keys = Object.keys(ref);
let count = 0;
return {
next: function() {
return {
value: ref[keys[count++]],
done: (count > keys.length -1)
}
}
}
}
})
const it = obj[Symbol.iterator]();
it.next(); // {value: 1, done: false}
it.next(); // {value: 2, done: false}
it.next(); // {value: 3, done: true}
it.next(); // {value: undefined, done: true}
for(let item of obj) {
console.log(item);
}