55
66let obj = {
77 a : 10 ,
8- b : 20
8+ b : 20 ,
99} ;
1010
1111let obj2 = {
1212 a : 10 ,
13- b : 20
13+ b : 20 ,
1414} ;
1515
1616console . log ( obj == obj2 ) ;
@@ -69,32 +69,35 @@ let EXAM = [
6969 Subeject : "English" ,
7070 result : {
7171 point : 80 ,
72- grade : "A+"
73- }
72+ grade : "A+" ,
73+ } ,
7474 } ,
7575 {
7676 Subeject : "Physics" ,
7777 result : {
7878 point : 85 ,
79- grade : "A+"
80- }
79+ grade : "A+" ,
80+ } ,
8181 } ,
8282 {
8383 Subeject : "Math" ,
8484 result : {
8585 point : 89 ,
86- grade : "A+"
87- }
88- }
86+ grade : "A+" ,
87+ } ,
88+ } ,
8989] ;
9090
9191let totalRes = 0 ;
92- EXAM . map ( subject => ( totalRes += subject . result . point ) ) ;
92+ EXAM . map ( ( subject ) => ( totalRes += subject . result . point ) ) ;
9393
9494console . log ( totalRes ) ;
9595console . log ( ( totalRes / EXAM . length ) . toFixed ( 2 ) ) ;
9696
97- let res = EXAM . reduce ( ( accumulator , currentValue ) => accumulator + currentValue . result . point , 0 ) ;
97+ let res = EXAM . reduce (
98+ ( accumulator , currentValue ) => accumulator + currentValue . result . point ,
99+ 0
100+ ) ;
98101console . log ( res ) ;
99102
100103/**
@@ -105,70 +108,84 @@ console.log(res);
105108
106109let obj1 = {
107110 a : 10 ,
108- b : 20
111+ b : 20 ,
109112} ;
110113
111114let obj2 = {
112115 c : 30 ,
113- d : 20
116+ d : 20 ,
114117} ;
115118
116119obj1 = { ...obj1 , ...obj2 } ;
117120console . log ( obj1 ) ;
118121
119-
120122// more test
121123const a = [ 1 , 2 , 5 , 7 , 9 ] ;
122124const b = [ 2 , 5 , 7 , 12 , 100 ] ;
123125
124126// const c = [...a, ...b];
125127
126- const c = a . concat ( b ) . sort ( ( a , b ) => a > b )
127-
128- console . log ( c )
128+ const c = a . concat ( b ) . sort ( ( a , b ) => a > b ) ;
129129
130+ console . log ( c ) ;
130131
131132const obj = {
132133 x : 1 ,
133134 getX ( ) {
134- const inner = function ( ) {
135- console . log ( this . x ) ;
136- }
135+ const inner = function ( ) {
136+ console . log ( this . x ) ;
137+ } ;
137138
138- inner . bind ( this ) ( ) ;
139- }
140- }
139+ inner . bind ( this ) ( ) ;
140+ } ,
141+ } ;
141142
142143obj . getX ( ) ;
143144
144- const arrayTotal = a . reduce ( ( t , i ) => t + i ) ;
145-
146- console . log ( arrayTotal )
145+ const arrayTotal = a . reduce ( ( t , i ) => t + i ) ;
147146
147+ console . log ( arrayTotal ) ;
148148
149149// OUTPUT
150150const arr = [ 1 , 2 , 3 , 4 , 5 ] ;
151151
152- arr . push ( arr . push ( arr . push ( arr . pop ( ) ) ) )
152+ arr . push ( arr . push ( arr . push ( arr . pop ( ) ) ) ) ;
153153
154154console . log ( arr ) ;
155155
156-
157156// OUTPUT
158157const arrayOfOddNumbers = [ 1 , 3 , 5 ] ;
159158arrayOfOddNumbers [ 100 ] = 199 ;
160159console . log ( arrayOfOddNumbers . length ) ;
161160
162-
163-
164161// OUTPUT
165162class MyClass extends ( String , Array ) {
166163 construct ( ) { }
167164}
168165
169- const a = new MyClass ( )
166+ const a = new MyClass ( ) ;
170167
171168console . log ( a instanceof Array ) ; // true
172169
173170// OUTPUT
174- [ "1101100000111110" , "1101110100011111" ] . map ( s => String . fromCharCode ( parseInt ( s , 2 ) ) ) . reduce ( ( acc , n ) => acc + n , "" ) ;
171+ [ "1101100000111110" , "1101110100011111" ]
172+ . map ( ( s ) => String . fromCharCode ( parseInt ( s , 2 ) ) )
173+ . reduce ( ( acc , n ) => acc + n , "" ) ;
174+
175+ // Preserve immutability of objects
176+
177+ const heroes = [
178+ { name : "Wolverine" , family : "Marvel" } ,
179+ { name : "Batman" , family : "DC Comics" } ,
180+ ] ;
181+
182+ const newHeroes = heroes . map ( ( hero ) => ( hero . name = hero . name . toUpperCase ( ) ) ) ;
183+
184+ console . log ( heroes ) ;
185+
186+ // Right way
187+ const newHeroes2 = heroes . map ( ( hero ) =>
188+ Object . assign ( { } , hero , { name : hero . name . toUpperCase ( ) } )
189+ ) ; // or return {...h, name: h.name.toUpperCase()};
190+
191+ console . log ( heroes ) ;
0 commit comments