4
4
5
5
// Imperative
6
6
7
- const marcus = {
7
+ const marcus1 = {
8
8
name : 'Marcus Aurelius' ,
9
9
born : 121 ,
10
+ upperName ( ) {
11
+ this . name = this . name . toUpperCase ( ) ;
12
+ } ,
10
13
get era ( ) {
11
14
return this . born < 0 ? 'BC' : 'AD' ;
12
15
} ,
@@ -15,13 +18,41 @@ const marcus = {
15
18
} ,
16
19
} ;
17
20
18
- console . log ( `${ marcus } ` ) ;
21
+ marcus1 . name += ' Antoninus' ;
22
+ marcus1 . city = 'Roma' ;
23
+ marcus1 . position = 'emperor' ;
24
+ marcus1 . upperName ( ) ;
25
+ console . log ( `Era of ${ marcus1 . name } birth is ${ marcus1 . era } ` ) ;
26
+ console . log ( `${ marcus1 } ` ) ;
27
+ const keys = Object . keys ( marcus1 ) ;
28
+ console . log ( 'Fields:' , keys . join ( ', ' ) ) ;
19
29
20
30
// Functional
21
31
22
32
const era = year => ( year < 0 ? 'BC' : 'AD' ) ;
23
33
const person = ( name , born ) => ( ) => `${ name } was born in ${ born } ${ era ( born ) } ` ;
24
34
25
35
const marcus2 = person ( 'Marcus Aurelius' , 121 , 'Roma' ) ;
26
-
27
36
console . log ( marcus2 ( ) ) ;
37
+
38
+ // Object reflection
39
+
40
+ const omap = ( obj , fn ) => Object . keys ( obj )
41
+ . reduce ( ( res , key ) => {
42
+ const [ prop , val ] = fn ( key , obj [ key ] ) ;
43
+ res [ prop ] = val ;
44
+ return res ;
45
+ } , { } ) ;
46
+
47
+ const marcus3 = {
48
+ firstName : 'Marcus' ,
49
+ middleName : 'Aurelius' ,
50
+ lastName : 'Antoninus' ,
51
+ } ;
52
+
53
+ const marcus4 = omap ( marcus3 , ( key , val ) => {
54
+ const prop = key . toLowerCase ( ) . replace ( 'name' , '' ) ;
55
+ const value = val . toUpperCase ( ) ;
56
+ return [ prop , value ] ;
57
+ } ) ;
58
+ console . log ( marcus4 ) ;
0 commit comments