-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoops.js
193 lines (150 loc) · 4.46 KB
/
oops.js
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
// that oops is concept from javascript
// Object, classes, encapsulation and Inheritance
// encapsulation : hiding the properties from the object which is created
// Inheritance : parent and child class relationships
// object
// In Javscript everything is an object
var a = 2; // object
function add(a, b) {
return a + b;
} // object
// object is an unique entity which contains property and methods
//Object literal
// Defining an Object
let person = {
first_name: "Sobyalal",
last_name: "Baby",
getFunction: function () {
return `${this.first_name} ${this.last_name}`;
},
phoneNumber: {
mobile: "12345677843",
landline: "122123123",
},
};
console.log(person.getFunction());
console.log(person.phoneNumber.landline);
// Using an Object Constructor
function personusingCons(first_name, last_name) {
this.first_name = first_name;
this.last_name = last_name;
console.log(`${this.first_name}....${this.last_name}`);
}
// constructor is a first executable function when object is instantiated
// create 2 persons
let person1 = new personusingCons("Sobyalal", "Baby"); // creating an new instance from object person
let person2 = new personusingCons("Chipui", "Kasar");
// Object.create() method
const coder = {
isStudying: false,
introduction: function () {
console.log(`My name is ${this.name} ${this.isStudying}`);
},
}; // object
const coder1 = Object.create(coder);
coder1.name = "Thangam";
coder1.isStudying = true;
coder1.introduction();
// Classes
// classes are exact blueprint of object
// class can have many objects because class is just a template while object are instances
// gold smith example
// there are no classes in javascript even if we define a class it will be an object internally
// Javascript is a prototype based object oriented programming language
// it is defining behaviors using constructor functions and then reuse that prototype
// ES2015 until that there was no keyword called class
// es6 they introduced traditional way of defining objects
// class keyword
class Vehicle {
constructor(name, maker, engine) {
this.name = name;
this.maker = maker;
this.engine = engine;
}
getDetails() {
return `${this.name}, ${this.maker}, ${this.engine}`;
}
}
let bike1 = new Vehicle("Hayabusa", "Suzuki", "1340cc");
let bike2 = new Vehicle("karizma", "Honda", "998cc");
console.log(bike1.name);
console.log(bike2.name);
// es2015 method
function VehicleOldWay(name, maker, engine) {
this.name = name;
this.maker = maker;
this.engine = engine;
}
VehicleOldWay.prototype.getDetails = function () {
return `${this.name}, ${this.maker}, ${this.engine}`;
};
let bike3 = new VehicleOldWay("Hayabusa", "Suzuki", "1340cc");
let bike4 = new VehicleOldWay("karizma", "Honda", "998cc");
console.log(bike3.name);
console.log(bike4.name);
console.log(bike3.getDetails());
// encapsulation
// process of wrapping or hiding property or function within a single class
class persont {
constructor(name, id) {
let addressa = "";
this.name = name;
this.id = id;
}
add_address(address) {
this.addressa = address;
}
getDetails() {
console.log(`${this.name} + ${this.add}`);
}
}
let personv = new persont("Vignesh", 21);
personv.add_address("Delhi");
console.log(personv.addressa);
// Inheritance
// Car --> driving() , usingBreaks() ---> common functionality ---> parent class --> inherit features to the child
// class
// Maruthi Suzuki --> driving() , using Breaks()
// benz --> driving() , using Breaks()
class Car {
constructor(steering, carbreak, gear) {
this.steering = steering;
this.carbreak = carbreak;
this.gear = gear;
}
// function
driving() {
return `we are using ${this.steering}, ${this.carbreak}, ${this.gear}`;
}
}
class maruthi extends Car {
constructor(steering, carbreak, gear, music) {
super(steering, carbreak, gear);
this.music = music;
}
driving() {
return `${super.driving()}, ${this.music}`;
}
}
let car1 = new Car("normalSteering", "normalCarbreak", "normalgear");
console.log(car1.driving());
let maruthi1car = new maruthi("boschsteering", "mitshubi", "havells", "harman");
console.log(maruthi1car.driving());
// class person {
// // parent class
// constructor(name) {
// this.name = name;
// }
// toString() {
// return `The name of the person is ${name}`;
// }
// }
// class student extends person {
// constructor(name, id) {
// super(name);
// this.id = id;
// }
// toString(){
// return
// }
// }