-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsec12-JavaScriptClasses.js
More file actions
162 lines (124 loc) · 3.24 KB
/
sec12-JavaScriptClasses.js
File metadata and controls
162 lines (124 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// THE SET UP
// new folder
// tsc --init
// npm init -y
// npm install lite-server
// (nodemon basically)
// index.html
// dist folder (for outputs)
// src folder
// indea.html(
// <script src='./dist/index.js
// '
// )
// config:
// outdir = /.dist
// tsc -w
// (watch)
// package.json:
// scripts:{
// 'start':'lite-server'
// }
// npm start
// SERVER RUNNING :D
// ///////////////////
// JS Classes:
//class keywords:
// terms: player1/players2= instances
// terms: Player= class
class Player {
lives=3
score=0
constructor (name,platform) {
this.name=name;
this.platform=platform;
// this.lives=3 //the method above the constructor is the same BUT neater!
}
gloat(){
console.log('Ohhhh yeaaah! look at that!')
}
getPoint(){
this.score+=1
}
loseLife(){
this.lives-=1
}
}
const player1 = new Player("Andy", "Xbox",);
const player2 = new Player("Meg", "Phone",);
console.log(player1)
console.log(player2)
//
player1.getPoint()
player2.loseLife()
//
console.log(player1)
console.log(player2)
//////////////
class YoutubeVideo{
views=0
constructor(creator,genre,ageRestriction,length){
this.creator=creator
this.genre=genre
this.ageRestriction=ageRestriction
this.length=length
}
checkUploadStatus(){
console.log('uploaded')
}
}
const memeVideo53 = new YoutubeVideo('FakeJake','Funny',false,600)
console.log(memeVideo53)
///////////////////////////////
//PRIVATE FIELDS///////////////
///////////////////////////////
//private fields: stop devs messing up values etc
// _score <underscore was to signal to devs to not change it
// #score <this makes it private, to update it a wrapper method can be used
class Meal {
static startMessage = console.log('a new meal created!') // static applies to every object in the class(NO! to the class no the instances.. i think), it's not unique to the object
#edable= true //this is private
constructor(type,gultenfree,cal,cost){
this.type=type
this.gultenfree=gultenfree
this.cal=cal
this.cost=cost
}
get checkEdabilty(){
console.log(`is it edable?`,this.#edable, '-checked with a getter')
}
set changeEdability(edable){
if (edable === true | false){
this.#edable = edable
console.log(`it's gone off! -private value changed with Setter`)
}
else{
throw new Error('it must be a boolean')
}
}
poisonous(){
console.log(`it's gone off! -private value changed with func()`)
this.#edable = false
}
}
const bigMac = new Meal ("fastfood",false,648,5.99)
console.log(bigMac)
bigMac.checkEdabilty
bigMac.poisonous()
bigMac.checkEdabilty
bigMac.changeEdability =true
bigMac.checkEdabilty
//Extending class & super()
class HomeCookedMeal extends Meal{
homeCooked='yes, obvs'
constructor (type,gultenfree,cal,cost,cook){
super(type,gultenfree,cal,cost,)
this.cook=cook
}
whoMadeIt(){
return `${this.cook} made the ${this.type}? ${this.homeCooked}`
}
}
const SundayRoast = new HomeCookedMeal("Traditional Roast",false,850,0,'Mum')
console.log(SundayRoast)
console.log(SundayRoast.whoMadeIt())