1
+ // classes: Main topic
2
+ // methods
3
+ // properties
4
+
5
+ // this
6
+ class Car {
7
+ constructor ( name , color , topSpeed ) {
8
+ // properties
9
+ this . name = name
10
+ this . color = color
11
+ this . topSpeed = topSpeed
12
+ this . currentSpeed = 0 ;
13
+ }
14
+
15
+ // getters & setters
16
+ getCurrentSpeed ( ) {
17
+ return currentSpeed
18
+ }
19
+
20
+ zeroToSixty ( ) {
21
+ setTimeout ( ( ) => {
22
+ console . log ( 'pHEW! That was fast!' )
23
+ this . currentSpeed = 60 ;
24
+ console . log ( this . currentSpeed )
25
+ } , 3000 )
26
+ }
27
+
28
+ drive ( speed = 10 ) {
29
+ // console.log('just drove 2 miles!')
30
+ this . currentSpeed += speed
31
+ console . log ( `driving speed at ${ this . currentSpeed } mph` )
32
+ }
33
+
34
+ brake ( ) {
35
+ console . log ( 'braking!' )
36
+ this . currentSpeed -= 10
37
+ }
38
+
39
+ stop ( ) {
40
+ console . log ( 'coming to a screeching halt!' )
41
+ this . currentSpeed = 0
42
+ }
43
+ }
44
+
45
+ // porsche, 'yellow', 250
46
+
47
+ const ferrari = new Car ( 'ferrari' , 'red' , 250 )
48
+ ferrari . drive ( )
49
+ ferrari . drive ( )
50
+ ferrari . drive ( )
51
+ ferrari . drive ( )
52
+ ferrari . drive ( )
53
+ console . log ( ferrari . currentSpeed )
54
+ ferrari . brake ( )
55
+ ferrari . stop ( )
56
+ console . log ( ferrari . currentSpeed )
57
+
58
+ const porsche = new Car ( 'Porsche' , 'yellow' , 250 )
59
+ console . log ( porsche . name )
60
+ console . log ( porsche . color )
61
+ console . log ( porsche . topSpeed )
62
+ // const nums = [1, 2, 3, 4, 5]
63
+ // nums.forEach(_ => porsche.drive())
64
+ porsche . drive ( 40 )
65
+ porsche . drive ( 80 )
66
+ console . log ( porsche . currentSpeed )
67
+ porsche . zeroToSixty ( )
68
+ porsche . stop ( )
69
+ console . log ( porsche . currentSpeed )
70
+
71
+ // console.log(ferrari.name)
72
+ // console.log(ferrari.color)
73
+ // console.log(ferrari.topSpeed)
74
+
75
+ // console.log(ferrari.currentSpeed)
76
+
77
+ // ferrari.drive()
78
+ // ferrari.brake()
79
+
80
+ // console.log(ferrari.currentSpeed)
81
+ // ferrari.drive()
82
+ // console.log(ferrari.currentSpeed)
83
+ // ferrari.drive()
84
+ // ferrari.drive()
85
+ // console.log(ferrari.currentSpeed)
86
+ // ferrari.zeroToSixty()
87
+ // console.log(ferrari.currentSpeed)
88
+
89
+ // console.log(ferrari)
90
+
91
+ // you can only have methods inside of classes
92
+ // const numbers = [1, 2, 3]
93
+ // numbers.push(4) // method
94
+ // // console.log(typeof )
95
+ // console.log(numbers)
96
+
97
+ Array . prototype . myPush = function ( item ) {
98
+ this [ this . length ] = item
99
+ return this
100
+ }
101
+
102
+ const fruits = [ '🍌' , '🍓' , '🍪' , '🍐' , '🍎' ]
103
+ fruits . myPush ( '🥝' )
104
+ fruits . myPush ( '🍪' )
105
+ fruits . myPush ( '🍓' )
106
+ fruits . myPush ( '🍊' )
107
+ console . log ( fruits )
0 commit comments