1- //classes
2-
3- // function Car(options) {
4- // this.title = options.title;
5- // }
6- // Car.prototype.drive = function() {
7- // return 'vroom';
8- // };
9- //
10- // function Toyota(options) {
11- // Car.call(this, options);
12- // this.color = options.color;
13- // }
14- //
15- //
16- // Toyota.prototype = Object.create(Car.prototype);
17- // Toyota.prototype.constructor = Toyota;
18- //
19- // Toyota.prototype.honk = function() {
20- // return 'beep';
21- // };
22- //
23- //
24- // const toyota = new Toyota({color: 'red', title: 'Daily Driver'});
25- //
26- // console.log(toyota.honk());
27- // console.log(toyota.drive());
28- // console.log(toyota.color);
29- // console.log(toyota.title);
30-
31- //es6
32-
33- class Car {
34- //could do {title} instead of options and this.title = title;
35- constructor ( options ) {
36- this . title = options . title ;
1+ //Generator
2+ // a generator can be entered and exited multiple times. We can return a value and then go right back into it
3+ //and keep going from the same location that we left.
4+
5+ const colors = [ 'red' , 'green' , 'blue' ] ;
6+
7+ //for/of loop
8+ for ( let color of colors ) {
9+ console . log ( color ) ;
10+ }
11+
12+ //the * makes this a generator
13+ function * shopping ( ) {
14+ //stuff on the sidewalk
15+
16+ // walking down the sidewalk
17+
18+ //go into the store with cash, //this is where we also reenter the generator
19+ //with 'groceries' yield is a keyword that exits function and then when
20+ //going back into the function we return to this place.
21+ const stuffFromStore = yield 'cash' ;
22+ //walking to laundrymat
23+ const cleanClothes = yield 'laundry' ;
24+ // walking back home
25+ return [ stuffFromStore , cleanClothes ] ;
26+ }
27+
28+ //stuff in the store
29+ //this doesn't actually go through the function
30+ const gen = shopping ( ) ;
31+ //gives {'done': false} leaving our house, this is where we actually go into
32+ //the function.
33+ gen . next ( ) ;
34+ //walked into the store at this point
35+ //walking through the store up and down the aisles
36+ //purchased our stuff
37+ // gives {'done': true} leaving the store with groceries
38+ gen . next ( 'groceries' ) ;
39+ gen . next ( 'clean clothes' ) ;
40+
41+
42+ function * colors ( ) {
43+ yield 'red' ;
44+ yield 'green' ;
45+ yield 'blue' ;
46+ }
47+
48+ const myColors = [ ] ;
49+ for ( let color of colors ( ) ) {
50+ myColors . push ( color ) ;
51+ }
52+
53+ myColors ;
54+
55+
56+ const engineeringTeam = {
57+ size : 3 ,
58+ dept : 'Engineering' ,
59+ lead : 'Jill' ,
60+ manager : 'Alex' ,
61+ engineer : 'Dave'
62+ }
63+
64+ function * TeamIterator ( team ) {
65+ yield team . lead ;
66+ yield team . manager ;
67+ yield team . engineer ;
68+ }
69+
70+ const names = [ ] ;
71+ for ( let name of TeamIterator ( engineeringTeam ) ) {
72+ names . push ( name ) ;
73+ }
74+
75+ names ;
76+
77+ //refractored
78+ const testingTeam = {
79+ lead : 'Amanda' ,
80+ tester : 'Bill' ,
81+ //the Symbol.iterator gets used if it exists to use the generator it is
82+ //pointing at to iterate over the object. the Symbol.iterator just teaches
83+ //the for/of loop how to iterate over the object. Note the use of this as
84+ //we are referring to the object that this generator is within.
85+ [ Symbol . iterator ] : function * ( ) {
86+ yield this . lead ;
87+ yield this . tester ;
3788 }
38- drive ( ) {
39- return 'vroom' ;
89+ }
90+
91+ const engineeringTeam = {
92+ size : 3 ,
93+ dept : 'Engineering' ,
94+ lead : 'Jill' ,
95+ manager : 'Alex' ,
96+ engineer : 'Dave' ,
97+ //could just be testingTeam by itself since it's key value is same
98+ testingTeam : testingTeam ,
99+ [ Symbol . iterator ] : function * ( ) {
100+ yield this . lead ;
101+ yield this . manager ;
102+ yield this . engineer ;
103+ //this yield causes the for/of loop to iterate over the team.testingTeam
104+ yield * this . testingTeam ;
40105 }
41106}
42107
43- //extends says we are grabbing a class Car and being able to use all it's methods
44- //super() allows us to call Car.constructor() because they have the same name. If both had honk() methods, we could
45- //declare super() inside the honk() method as well. We also have to pass super any info the Car.constructor()
46- //may need as well.
47- //Also to note you don't want to use destrucutring to pull off what you want, you need to have them all grouped
48- //together within the object.
49- class Toyota extends Car {
50- constructor ( options ) {
51- super ( options ) ;
52- this . color = options . color ;
108+ const names = [ ] ;
109+ for ( let name of engineeringTeam ) {
110+ names . push ( name ) ;
111+ }
112+ names ;
113+
114+
115+ //tree
116+
117+ class Comment {
118+ constructor ( content , children ) {
119+ this . content = content ;
120+ this . children = children ;
53121 }
54- honk ( ) {
55- return 'beep' ;
122+ * [ Symbol . iterator ] ( ) {
123+ yield this . content ;
124+ for ( let child of this . children ) {
125+ yield * child ;
126+ }
56127 }
57128}
58129
59- const toyota = new Toyota ( { color : 'red' , title : 'Daily Driver' } ) ;
60- console . log ( toyota . honk ( ) ) ;
61- console . log ( toyota . drive ( ) ) ;
62- console . log ( toyota . color ) ;
63- console . log ( toyota . title ) ;
130+ const children = [
131+ new Comment ( 'good comment' , [ ] ) ,
132+ new Comment ( 'bad comment' , [ ] ) ,
133+ new Comment ( 'meh' , [ ] )
134+ ] ;
135+
136+ const tree = new Comment ( 'Great post!' , children ) ;
64137
138+ const values = [ ] ;
139+
140+ for ( let value of tree ) {
141+ values . push ( value ) ;
142+ }
65143
144+ values ;
0 commit comments