1- //destructuring 
2- 
3- var  expense  =  { 
4-     type : 'Business' , 
5-     amount : '$45 USB' 
6- } 
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+ // 
723// 
8- // var type = expense.type; 
9- // var amount = expense.amount; 
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); 
1030
1131//es6 
12- //when curly braces used on left side, it is saying you want to declare a new variable called whatever is inside the 
13- //brace and it should reference the right hand expression. 
14- 
15- //here we are saying we want to declare a variable called type and assign it to expense.type. 
16- // const { type } = expense; 
17- // const { amount } = expense; 
18- 
19- //can simplify even more, we can pull off more than one variable within an object with a comma separated list 
20- const  {  type,  amount }  =  expense ; 
21- 
2232
23- var   savedFile   =  { 
24-     extension :  '.jpg' , 
25-     name :  'repost' , 
26-     size :  14040 
27- } ; 
28- 
29- function   fileSummary ( file )   { 
30-     return   `The file  ${ file . name } ${ file . extension }  is of size  ${ file . size } ` 
33+ class   Car  { 
34+     //could do {title} instead of options and this.title = title; 
35+     constructor ( options )   { 
36+          this . title   =   options . title ; 
37+      } 
38+      drive ( )   { 
39+          return   'vroom' ; 
40+     } 
3141} 
3242
33- console . log ( fileSummary ( savedFile ) ) ; 
34- 
35- //destructure 
36- 
37- function  fileSummary1 ( {  name,  extension,  size} )  { 
38-     return  `The file ${ name } ${ extension } ${ size }  
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 ; 
53+     } 
54+     honk ( )  { 
55+         return  'beep' ; 
56+     } 
3957} 
4058
41- console . log ( fileSummary1 ( savedFile ) ) ; 
42- 
43- //arrays 
44- 
45- //when desstructing arrays, you get them in the order they are in the original array 
46- const  companies  =  [ 'google' ,  'facebook' ,  'Uber' ] ; 
47- // 
48- // const [ name, name1, name2 ] = companies; 
49- // console.log(name, name1, name2); 
50- 
51- 
52- const  companies2  =  [ 
53-     { name : 'google' ,  location : 'Mountain View' } , 
54-     { name : 'facebook' ,  location : 'Menlo Park' } , 
55-     { name : 'Uber' ,  location : 'San Francisco' } 
56- ] ; 
57- 
58- //goes outside in. so first we grab the first index of the array which is the full obj. then, with the {} we 
59- //are destructuring further and getting the name and location. 
60- const  [ { name,  location} ]  =  companies2 ; 
61- console . log ( name ,  location ) ; 
62- 
63- 
64- const  Google  =  { 
65-     locations : [ 'Mountain View' ,  'New York' ,  'London' ] 
66- } ; 
67- //more chanllenging...we are first destructing obj. and getting locations. However, this is an array so we need 
68- //to walk into the array and pull off the first item of that array 
69- // const {locations : [ location ]} = Google; 
70- // console.log(location); 
71- 
72- const  points  =  [ 
73-     [ 4 , 5 ] , 
74-     [ 10 ,  1 ] , 
75-     [ 0 ,  40 ] 
76- ] ; 
77- 
78- const  list  =  points . map ( ( [ x ,  y ] )  =>  { 
79-     return  {  x : x ,  y : y } ; 
80- } ) ; 
81- 
82- console . log ( list ) ; 
83- 
84- 
85- const  numbers  =  [ 1 ,  2 ,  3 ] ; 
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 ) ; 
8664
87- const  double  =  ( [ num ,  ...rest ] )  =>  rest . length ? [  num  * 2 ,  ...double ( rest )  ]  : [  num  *  2  ] ; 
8865
89- double ( numbers ) ; 
0 commit comments