You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: part6 (Object-Oriented Programming)/05_classes.js
+76-30Lines changed: 76 additions & 30 deletions
Original file line number
Diff line number
Diff line change
@@ -57,40 +57,59 @@ car.details(); // Supra - 2020
57
57
58
58
/*
59
59
🔹 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.
62
61
*/
63
-
constcar1=newCar("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
+
classUser{
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
+
classUser{
72
+
constructor(name){
73
+
this.name=name;
74
+
}
75
+
76
+
greet(){
77
+
console.log(`Hi, I'm ${this.name}`);
78
+
}
79
+
}
80
+
81
+
constuser1=newUser("Alice");
82
+
user1.greet();// "Hi, I'm Alice"
66
83
67
84
/*
68
85
🔹 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.
70
90
*/
71
91
classAnimal{
72
92
constructor(name){
73
93
this.name=name;
74
94
}
75
95
76
96
speak(){
77
-
console.log(`${this.name} makes a noise.`);
97
+
console.log(`${this.name} makes a sound.`);
78
98
}
79
99
}
80
100
81
101
classDogextendsAnimal{
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
85
104
}
86
105
87
106
speak(){
88
107
console.log(`${this.name} barks.`);
89
108
}
90
109
}
91
110
92
-
constd=newDog("Rex","Labrador");
93
-
d.speak();// Rex barks.
111
+
constdog=newDog("Buddy");
112
+
dog.speak();// "Buddy barks."
94
113
95
114
/*
96
115
🔹 6. Method Overriding
@@ -107,39 +126,49 @@ class Child extends Parent {
107
126
console.log("Hello from child");
108
127
}
109
128
}
129
+
constnewChild=newChild();
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.
110
132
111
133
/*
112
134
🔹 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.
114
137
*/
115
-
classMathUtils{
116
-
staticsquare(x){
117
-
returnx*x;
138
+
classMathHelper{
139
+
staticadd(a,b){
140
+
returna+b;
118
141
}
119
142
}
120
143
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.
122
146
123
147
/*
124
148
🔹 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.
126
150
*/
127
-
classCounter{
128
-
#count =0;
151
+
classUser{
152
+
#password;
129
153
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
+
returnpw===this.#password;
133
161
}
134
162
}
135
163
136
-
constc=newCounter();
137
-
c.increment();// 1
138
-
// c.#count; // ❌ Error: Private field '#count' must be declared
0 commit comments