回答編集履歴
2
Symbol.iterator
test
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
### new Map
|
2
|
+
|
1
3
|
> ```javascript
|
2
4
|
> return schedules = {
|
3
5
|
> title: title,
|
@@ -29,4 +31,30 @@
|
|
29
31
|
for (let [key, value] of schedule) console.log(key + ': ' + value);
|
30
32
|
```
|
31
33
|
|
34
|
+
### Symbol.iterator
|
35
|
+
|
36
|
+
`Symbol.iterator` を使用することで、iterableなオブジェクトを生成することができます。
|
37
|
+
|
38
|
+
- [Symbol\.iterator \- JavaScript \| MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Symbol/iterator)
|
39
|
+
|
40
|
+
```javascript
|
41
|
+
'use strict';
|
42
|
+
class Schedule {
|
43
|
+
constructor (title, startHour, startMinute, endHour, endMinute) {
|
44
|
+
this.title = title;
|
45
|
+
this.startHour = startHour;
|
46
|
+
this.startMinute = startMinute;
|
47
|
+
this.endHour = endHour;
|
48
|
+
this.endMinute = endMinute;
|
49
|
+
}
|
50
|
+
[Symbol.iterator] = function* () {
|
51
|
+
for (let key of ['title', 'startHour', 'startMinute', 'endHour', 'endMinute']) {
|
52
|
+
yield [key, this[key]];
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
for (let [key, value] of new Schedule('lunch', 12, 0, 13, 0)) console.log(key + ': ' + value);
|
58
|
+
```
|
59
|
+
|
32
60
|
Re: atago_IT さん
|
1
問題のコードを引用
test
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
> ```javascript
|
2
|
+
> return schedules = {
|
3
|
+
> title: title,
|
4
|
+
> startHour: startHour,
|
5
|
+
> startMinute: startMinute,
|
6
|
+
> endHour: endHour,
|
7
|
+
> endMinute: endMinute
|
8
|
+
> };
|
9
|
+
> ```
|
10
|
+
|
1
11
|
「オブジェクトのプロパティ」は**iterable**ではありません。
|
2
12
|
|
3
13
|
- [反復処理プロトコル \- JavaScript \| MDN](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Iteration_protocols)
|