forked from bittu1040/JavaScript-Coding-and-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoops-in-javascript.js
More file actions
218 lines (156 loc) · 4.76 KB
/
oops-in-javascript.js
File metadata and controls
218 lines (156 loc) · 4.76 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// encaptulation
/*
Data Hiding / Encapsulation in JavaScript.
Data Hiding/Encapsulation in JavaScript can be easily achieved by the help of Closure.
Encapsulation is a fundamental concept of Object oriented programming.
This helps us to hide internal details of object from other objects.
JavaScript uses closures to implement encapsulation, which allows developers to create private variables and methods that cannot be accessed from outside the object.
*/
// example 1
// we can't access count variable from outside,
/*
function counter(){
let count=0;
return function(){
return count++;
}
}
let count= counter()
console.log(count())
console.log(count())
// example 2
function personDetails(name, age){
let privateAge= age;
this.name= name;
this.getAge= function(){
return privateAge;
}
}
let person1= new personDetails("Bittu", 22);
console.log(person1.getAge())
*/
// inheritance :
// Inheritance in JavaScript is a fundamental concept of Object oriented programming.
// child class can inherit all the functionalities of the parent's class
// this allows code reusability
// inheritance can be achieved in two ways:
// class based and prototype based.
// class based inheritance
/*
class Car{
constructor(name, brand, color){
this.name= name;
this.brand= brand;
this.color= color;
}
getCarDetails(){
return this.name + " " + this.brand + " " + this.color
}
start(){
return "car is started";
}
}
let car1= new Car("800", "maruti", "red" )
console.log(car1);
class electricCar extends Car{
constructor( name, brand, color, batteryCapacity){
super(name, brand, color)
this.batteryCapacity= batteryCapacity;
}
}
let car2= new electricCar("Nexon","Tata","Blue","100AH")
Car.prototype.testMethod= function(){ // custom method added in prototype which is available to all the instances
return "test method"
}
console.log(car2.testMethod());
console.log(car1.testMethod());
*/
/*
function Car(name, brand, color){
this.name= name;
this.brand= brand;
this.color= color;
}
Car.prototype.start= function(){
return "car is started"
}
let car1= new Car("800", "maruti", "red" )
Car.prototype.start= function(){
return "car is started ........"
}
console.log(car1.start());
console.log(car1.name)
*/
// prototype based inheritance:
function Person(name, age){
this.name=name;
this.age= age
}
Person.prototype.getDetails= function(){
return this.name+ " " + this.age;
}
function Student(name, age, major){
Person.call(this, name, age)
this.major= major;
}
//Student.prototype inherits from Person.prototype
Student.prototype= Object.create(Person.prototype);
console.log(Student.prototype.constructor); // Person
// This should not point to Person, It should point to Student
// So that the reason we reset the constructor property of Student.prototype
Student.prototype.constructor= Student;
console.log(Student.prototype.constructor) // Student : now its point to Student
Student.prototype.testMethod= function(){
return "test method"
}
let person1= new Person("suraj", 26);
let student1= new Student("Bittu", 25, "CS");
console.log(student1.getDetails()) // "Bittu 25"
console.log(student1.major); // "CS"
// abstraction
// Abstraction involves hiding the complexity of the implementation and showing only the necessary features of an object.
// It allows programmers to focus on the essential aspects of an object while hiding the irrelevant details.
// In JavaScript, abstraction can be achieved through encapsulation
// To remember this: we can take example of car- we only know to drive, we don't know how car is running.
// and me also dont want to know how this car drive is happening behind the scene.
// Lets see the example with the same car:
/*
class Car{
constructor(name, brand, color){
this.name= name;
this.brand= brand;
this.color= color;
}
startCar(){
console.log(`${this.name} is started`);
}
stop(){
console.log(`${this.name} is stopped`);
}
}
let car1= new Car("800", "maruti", "red" )
car1.startCar();
car1.stop();
*/
// polymorphism
// This allows methods to behave differently depending on the specific type of object they are called on.
// Polymorphism allows different objects to respond to the same method or property in his own way.
class Shape {
constructor(name) {
this.name = name;
}
draw() {
console.log(`Drawing a ${this.name}`);
}
}
class Circle extends Shape {
draw() {
console.log(`Drawing a Circle`);
}
}
let shape = new Shape("Shape");
let circle = new Circle("nn");
console.log(shape)
console.log(circle)
shape.draw();
circle.draw();