Skip to content

Commit c53ddcb

Browse files
feat: add updated classes.js file
1 parent e6bb0bb commit c53ddcb

File tree

1 file changed

+76
-30
lines changed

1 file changed

+76
-30
lines changed

part6 (Object-Oriented Programming)/05_classes.js

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,40 +57,59 @@ car.details(); // Supra - 2020
5757

5858
/*
5959
🔹 4. Instance Properties & Methods
60-
Instance properties are attached to (this) inside constructor.
61-
Instance methods are added to the prototype, not copied per instance.
60+
In JavaScript classes, when you create an object using new, you get an instance of that class. The values and functions (methods) that belong to this instance are called instance properties and instance methods.
6261
*/
63-
const car1 = new Car("Toyota", 2022);
64-
console.log(car1.model); // Toyota
65-
car1.details(); // Toyota - 2022
62+
63+
// ✅ Instance Properties: Declared usually inside the constructor function using this.
64+
class User {
65+
constructor(name) {
66+
this.name = name; // instance property
67+
}
68+
}
69+
70+
// ✅ Instance Methods: These are functions defined in the class body and are available to all instances via the prototype.
71+
class User {
72+
constructor(name) {
73+
this.name = name;
74+
}
75+
76+
greet() {
77+
console.log(`Hi, I'm ${this.name}`);
78+
}
79+
}
80+
81+
const user1 = new User("Alice");
82+
user1.greet(); // "Hi, I'm Alice"
6683

6784
/*
6885
🔹 5. Inheritance in JavaScript (extends, super)
69-
✅ Basic Inheritance:
86+
✅ extends
87+
Used to create a class that inherits from another class.
88+
✅ super()
89+
Used to call the parent class’s constructor and access its methods.
7090
*/
7191
class Animal {
7292
constructor(name) {
7393
this.name = name;
7494
}
7595

7696
speak() {
77-
console.log(`${this.name} makes a noise.`);
97+
console.log(`${this.name} makes a sound.`);
7898
}
7999
}
80100

81101
class Dog extends Animal {
82-
constructor(name, breed) {
83-
super(name); // calls the parent constructor
84-
this.breed = breed;
102+
constructor(name) {
103+
super(name); // Calls the parent constructor
85104
}
86105

87106
speak() {
88107
console.log(`${this.name} barks.`);
89108
}
90109
}
91110

92-
const d = new Dog("Rex", "Labrador");
93-
d.speak(); // Rex barks.
111+
const dog = new Dog("Buddy");
112+
dog.speak(); // "Buddy barks."
94113

95114
/*
96115
🔹 6. Method Overriding
@@ -107,39 +126,49 @@ class Child extends Parent {
107126
console.log("Hello from child");
108127
}
109128
}
129+
const newChild = new Child();
130+
newChild.sayHello(); // Hello from child
131+
// The Child class overrides the Parent method. If the method exists in both, the child version is used.
110132

111133
/*
112134
🔹 7. Static Methods
113-
Static methods are called on the class itself, not on instances.
135+
Called on the class itself, not on instances.
136+
Often used for utility/helper functions.
114137
*/
115-
class MathUtils {
116-
static square(x) {
117-
return x * x;
138+
class MathHelper {
139+
static add(a, b) {
140+
return a + b;
118141
}
119142
}
120143

121-
console.log(MathUtils.square(4)); // 16
144+
console.log(MathHelper.add(3, 4)); // 7
145+
// You can’t do new MathHelper().add() — that would throw an error.
122146

123147
/*
124148
🔹 8. Private Fields and Methods (Truly Private)
125-
Introduced in ES2022, private fields start with #. They cannot be accessed outside the class.
149+
Use # to declare private fields. These cannot be accessed or modified outside the class.
126150
*/
127-
class Counter {
128-
#count = 0;
151+
class User {
152+
#password;
129153

130-
increment() {
131-
this.#count++;
132-
console.log(this.#count);
154+
constructor(name, password) {
155+
this.name = name;
156+
this.#password = password;
157+
}
158+
159+
checkPassword(pw) {
160+
return pw === this.#password;
133161
}
134162
}
135163

136-
const c = new Counter();
137-
c.increment(); // 1
138-
// c.#count; // ❌ Error: Private field '#count' must be declared
164+
const newU = new User("Alice", "secret");
165+
console.log(newU.#password); // ❌ Syntax Error
166+
console.log(newU.checkPassword("secret")); // ✅ true
139167

140168
/*
141169
🔹 9. Getters and Setters
142-
Allow controlled access to properties.
170+
getters => access properties
171+
setters => change (mutate) them
143172
*/
144173
class User {
145174
constructor(name) {
@@ -160,14 +189,31 @@ console.log(u.name); // ALICE
160189
u.name = " Bob ";
161190
console.log(u.name); // BOB
162191

192+
const user = {
193+
firstName: "Abdul",
194+
lastName: "Rafay",
195+
get fullName() {
196+
return `${this.firstName} ${this.lastName}`;
197+
},
198+
set fullName(value) {
199+
const parts = value.split(" ");
200+
this.firstName = parts[0];
201+
this.lastName = parts[1];
202+
},
203+
};
204+
205+
user.fullName = "John Smith";
206+
console.log(person);
207+
163208
/*
164209
🔹 10. instanceof Operator
210+
what is instace: When you create an object using new, like new User(), that object is an instance of the class.
165211
Used to check if an object is an instance of a class or constructor.
166212
*/
167213
class A {}
168214
class B extends A {}
169215

170216
let b = new B();
171-
console.log(b instanceof B); // true
172-
console.log(b instanceof A); // true
173-
console.log(b instanceof Object); // true
217+
console.log(b instanceof B); // true — b is instance of B
218+
console.log(b instanceof A); // true — B extends A
219+
console.log(b instanceof Object); // true — All classes inherit from Object

0 commit comments

Comments
 (0)