Skip to content

Commit 553a657

Browse files
feat: add classes, inheritance, static, getter & setters
1 parent 62a4505 commit 553a657

File tree

1 file changed

+138
-17
lines changed

1 file changed

+138
-17
lines changed

part6 (Object-Oriented Programming)/Classes_&_Inheritance.js/classes_&_inheritance.js

Lines changed: 138 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,24 @@ Inheritance allows one class to inherit from another class.
3434

3535
// Example
3636
class User {
37-
constructor(name) {
37+
constructor(name, age) {
3838
this.name = name;
39+
this.age = age;
3940
}
4041

4142
speak() {
42-
console.log(`${this.name} says hello.`);
43+
console.log(`${this.name} says hello. And his age is ${this.age}`);
4344
}
4445
}
4546

4647
class Developer extends User {
47-
constructor(name, language) {
48-
super(name); // calls the constructor of Person
48+
constructor(name, age, language) {
49+
super(name, age); // calls the constructor of Person & super must be called once.
4950
this.language = language;
5051
}
5152

5253
code() {
53-
console.log(`${this.name} writes code in ${this.language}`);
54+
console.log(`${this.name} writes code in ${this.language} & his age is ${this.age}`);
5455
}
5556
}
5657

@@ -59,59 +60,179 @@ dev.speak(); // Rafay says hello.
5960
dev.code(); // Rafay writes code in JavaScript
6061

6162
/*
62-
INFO: What is static in classes ?
63-
A static method or property belongs to the class itself, not to any object created from it.
63+
INFO: What is static keyword in classes ?
64+
The static keyword in javascript classes is used to define properties or methods that belongs to the class itself, not to instances of the clases. Static members are accessed directly via the class name, not through an instance.
6465
*/
6566

6667
// Example
6768
class Car {
69+
static count = 0;
70+
6871
constructor(model) {
6972
this.model = model;
73+
Car.count++
7074
}
7175

7276
static company() {
7377
return "Toyota";
7478
}
79+
80+
static getCount() {
81+
return Car.count;
82+
}
7583
}
7684

85+
console.log(Car.count); // 1
7786
const myCar = new Car("BMW");
7887

7988
console.log(Car.company()); // Toyota
89+
console.log(Car.getCount()); // 2
8090
console.log(myCar.company()); // Error: myCar.company is not a function
8191

8292

83-
// INFO: get and set (Getters & Setters)
93+
/* INFO: get and set (Getters & Setters)
94+
Getters and setters in javascript are special methods that allow you to define custom behaviour when accessing or setting object properties.
95+
*/
8496

8597
/*
8698
INFO: What is getter ?
87-
A getter allows you to access a property like a variable, but it actually runs a fucntion behind the scenes.
99+
get lets you retrieve (read) a property like a variable, but it's actually a function.
88100
*/
89101

90102
/*
91103
INFO: What is setter ?
92-
A setter allows you to control how a property is set (add validation, logs, etc.)
104+
set lets you update (write) a property like a variable, but with custom logic behind it.
93105
*/
94106

95-
class Employee {
107+
// Syntax
108+
class MyClass {
96109
constructor(name) {
97-
this.name = name;
110+
this._name = name;
98111
}
99112

100113
get name() {
101-
return this._name.toUpperCase();
114+
return this._name;
115+
}
116+
117+
set name(newName) {
118+
this._name = newName;
119+
}
120+
}
121+
122+
// Convention: Use an underscore _name to store the actual value so it doesn't clash with the getter/setter.
123+
124+
// Example
125+
class Character {
126+
constructor(age) {
127+
this._age = age;
128+
}
129+
130+
get age() {
131+
console.log("Getting age...");
132+
return this._age;
133+
}
134+
135+
set age(value) {
136+
if (value < 0) {
137+
console.log("Age cannot be negative.");
138+
return;
139+
}
140+
console.log("Setting age...");
141+
this._age = value;
142+
}
143+
}
144+
145+
const char = new Character(17);
146+
147+
console.log(char.age); // Getting age... -> 17
148+
char.age = 30; // Setting age...
149+
console.log(char.age); // Getting age... -> 30
150+
151+
char.age = -5; // Age cannot be negative.
152+
console.log(char.age); // Getting age... -> 30 (unchanged)
153+
154+
155+
// Example of call concepts
156+
class Gadget {
157+
static totalGadgets = 0;
158+
159+
constructor(name, price) {
160+
this._name = name;
161+
this._price = price;
162+
Gadget.totalGadgets++;
163+
}
164+
165+
get name() {
166+
console.log("Reading name...");
167+
return this._name;
102168
}
103169

104170
set name(value) {
105-
if (value.length < 3) {
106-
console.log("Name is too short!");
171+
if (typeof value !== "string" || value.trim() === "") {
172+
console.log("Invalid name.");
107173
return;
108174
}
175+
console.log("Setting name...");
109176
this._name = value;
110177
}
178+
179+
get price() {
180+
console.log("Reading price...");
181+
return this._price;
182+
}
183+
184+
set price(value) {
185+
if (value < 0) {
186+
console.log("Price cannot be negative.");
187+
return;
188+
}
189+
this._price = value;
190+
}
191+
192+
static getTotalGadgets() {
193+
return Gadget.totalGadgets;
194+
}
195+
196+
describe() {
197+
console.log(`${this.name} costs $${this.price}`);
198+
}
199+
}
200+
201+
class Smartphone extends Gadget {
202+
constructor(name, price, os) {
203+
super(name, price);
204+
this._os = os;
205+
}
206+
207+
get os() {
208+
console.log("Reading os...");
209+
return this._os;
210+
}
211+
212+
set os(value) {
213+
if (!value) {
214+
console.log("OS cannot be empty.");
215+
return;
216+
}
217+
this._os = value;
218+
}
219+
220+
describe() {
221+
console.log(`${this.name} runs an ${this.os} and costs ${this.price}`);
222+
}
111223
}
112224

113-
const emp = new Employee("rafay");
114-
console.log(emp.name); // RAFAY
225+
const g1 = new Gadget("Bluetooth Speaker", 50);
226+
const phone1 = new Smartphone("Iphone 16 pro max", 49999, "unix");
227+
const phone2 = new Smartphone("Galaxy S22", 999, "Android");
115228

229+
phone1.price = -100; // Invalid
230+
phone1.price = 899;
231+
phone2.name = ""; // Invalid
232+
phone2.name = "Iphone 14 Pro";
116233

234+
g1.describe();
235+
phone1.describe();
236+
phone2.describe();
117237

238+
console.log(Gadget.getTotalGadgets()); // 3

0 commit comments

Comments
 (0)