-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterators.html
74 lines (54 loc) · 2.23 KB
/
iterators.html
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html>
<head>
<!-- Load babel 5 - babel-core -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.38/browser.js"></script>
<script type="text/babel">
var title = "ES6";
console.log(typeof title[Symbol.iterator]);
var iterateIt = title[Symbol.iterator]();
console.log(iterateIt.next());
console.log(iterateIt.next());
console.log(iterateIt.next());
console.log(iterateIt.next());
// Define custom iterator
function tableReady(arr) {
var nextIndex = 0;
return {
next() {
if (nextIndex < arr.length) {
return {value: arr.shift(), done: false}
}else {
return {done: true}
}
}
}
}
var waitingList = ['Dandy', 'Jefferson', 'Wenqian', 'Xu'];
var iterateList = tableReady(waitingList);
console.log(`${iterateList.next().value}, your table is ready.`);
console.log(`${iterateList.next().value}, your table is ready.`);
console.log(`${iterateList.next().value}, your table is ready.`);
console.log(`${iterateList.next().value}, your table is ready.`);
console.log(`Is your table is ready? ${iterateList.next().done}`);
</script>
<title>Iterators</title>
</head>
<body>
<h1>Hit the pit</h1>
<h2>Iterators</h2>
<ul>
<li>Processing items in a collection</li>
<li>Using for loops, while loops, and map()</li>
<li><strong>New:</strong> Custom iteration behaviour in ES6</li>
</ul>
<h2>New Protocols</h2>
<ul>
<li><strong>Iterable</strong> - JS objects define their own iteration behaviour.</li>
<li><strong>Iterator</strong> - a standard way to produce a sequence of values.</li>
</ul>
<ul>
<li>Symbol.iterator is a method that returns the default iterator for an object or a factory of iterators.</li>
</ul>
</body>
</html>