1- //fat arrows
2-
3- const add = function ( a , b ) {
4- return a + b ;
5- } ;
6-
7- add ( 1 , 2 ) ;
8-
9- //remove function key word add fat arrow after params
10- const addFat = ( a , b ) => {
11- return a + b ;
12- } ;
13-
14- addFat ( 1 , 2 ) ;
15-
16-
17- //if single expression, you can remove the brackets and the return and, it will implicitly return
18- const addFat1 = ( a , b ) => a + b ;
19-
20- addFat1 ( 1 , 2 ) ;
21-
22-
23- const double = function ( number ) {
24- return 2 * number ;
25- } ;
26-
27- double ( 8 ) ;
28-
29- //with a single argument coming in, you can also omit the () around it. like the following
30- // const double1 = number => return 2 * number;
31- //
32- // double1(8);
33-
34- const numbers = [ 1 , 2 , 3 ] ;
35-
36- //using map this is how learned initially
37- numbers . map ( function ( number ) {
38- return 2 * number
39- } ) ;
40- //this drops function and puts the fat arrow after argument. as only one arguement can drop ()
41- //since a single expression, can remove {} and return so it becomes just this
42- numbers . map ( number => 2 * number ) ;
43-
44- const team = {
45- members : [ 'Jane' , 'Bill' ] ,
46- teamName : 'Super Squad' ,
47- teamSummary : function ( ) {
48- return this . members . map ( member => {
49- return `${ member } is on team ${ this . teamName } `
50- } )
1+ function createBookShop ( inventory ) {
2+ return {
3+ inventory : inventory ,
4+ inventoryValue : function ( ) {
5+ return this . inventory . reduce ( ( total , book ) => total + book . price , 0 ) ;
6+ } ,
7+ priceForTitle : function ( title ) {
8+ return this . inventory . find ( book => book . title === title ) . price ;
9+ }
5110 }
52- } ;
11+ }
12+
13+ const inventory = [
14+ { title : 'Harry Potter' , price : 10 } ,
15+ { ttile : "Eloquent JS" , price : 15 }
16+ ] ;
5317
54- console . log ( team . teamSummary ( ) ) ;
18+ const bookShop = createBookShop ( inventory ) ;
0 commit comments