File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change 3
3
// Generator function
4
4
5
5
function * genFn ( x ) {
6
- return x * 2 ;
6
+ yield x * 2 ;
7
+ return x * 3 ;
7
8
}
8
9
9
10
console . log ( 'genFn =' , [ genFn ] ) ;
10
11
console . log ( 'genFn.toString() =' , [ genFn . toString ( ) ] ) ;
11
- console . log ( 'typeof( genFn) =' , typeof ( genFn ) ) ;
12
+ console . log ( 'typeof genFn =' , typeof genFn ) ;
12
13
const fnProto = Object . getPrototypeOf ( genFn ) ;
13
14
console . log ( 'fnProto.constructor.name =' , fnProto . constructor . name ) ;
14
15
15
- console . log ( 'typeof( genFn(5)) =' , typeof ( genFn ( 5 ) ) ) ;
16
+ console . log ( 'typeof genFn(5) =' , typeof genFn ( 5 ) ) ;
16
17
console . log ( 'genFn(5).toString() =' , genFn ( 5 ) . toString ( ) ) ;
17
18
const genProto = Object . getPrototypeOf ( genFn ( 5 ) ) ;
18
19
console . log ( 'genProto =' , genProto ) ;
@@ -28,9 +29,11 @@ class Multiplier {
28
29
constructor ( k ) {
29
30
this . value = k ;
30
31
}
32
+
31
33
* genMethod ( a ) {
32
- this . value = a * this . value ;
33
- return a * this . value ;
34
+ yield this . value ;
35
+ this . value = this . value * a ;
36
+ return this . value ;
34
37
}
35
38
}
36
39
@@ -41,8 +44,10 @@ console.log('m1.genMethod(5).next() =', m1.genMethod(5).next());
41
44
42
45
const m2 = {
43
46
value : 2 ,
47
+
44
48
* genMethod ( a ) {
45
- this . value = a * this . value ;
49
+ yield this . value ;
50
+ this . value = this . value * a ;
46
51
return this . value ;
47
52
}
48
53
} ;
You can’t perform that action at this time.
0 commit comments