-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOPs.js
More file actions
224 lines (169 loc) · 5.87 KB
/
Copy pathOOPs.js
File metadata and controls
224 lines (169 loc) · 5.87 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
214
215
216
217
218
219
220
221
222
223
224
// //Constructors and this keyword
// const John= {
// name: "John",
// age: 50,
// };
// console.log(John);
// function Person(name,age){
// this.name= name;
// this.age = age;
// console.log(this);
// }
// const Peter = new Person("Peter",18)
// const Alan = new Person("Alan",28)
// const Jack = new Person("Jack Sparrow",37)
// console.log(Person);
// const name1 = "John";
// const name2 = new String("Smith");
// name2.age = 50;
// console.log(name1, typeof name1);
// console.log(name2, typeof name2);
// if(name2 === "Smith"){
// console.log("Yes");
// }
// else{
// console.log("No");
// }
// const num1 = 5;
// const num2 = new Number(5);
// console.log(num1, typeof num1);
// console.log(num2, typeof num2);
// const bool1 = true;
// const bool2 = new Boolean(true);
// console.log(bool1, typeof bool1);
// console.log(bool2, typeof bool2);
// const getSum1 = function(x,y){
// return x+y;
// }
// console.log(getSum1(1,1));
// const getSum2 = new Function('x','y','return 1+1');
// console.log(getSum2(1,1));
// const john1 = {name: "John"};
// const john2 = new Object({name: "John"});
// console.log(john1, typeof john1);
// console.log(john2, typeof john2);
// const arr1 = [1,2,3,4];
// const arr2 = new Array(1,2,3,4);
// console.log(arr1, typeof arr1);
// console.log(arr2, typeof arr2);
// const re1 = /\w+/;
// const re2 = new RegExp('\\w+');
// console.log(re1, typeof re1);
// console.log(re2, typeof re2);
// //Prototypes save memory space
//Prototypes are used to add methods and properties to an object
// function Person(firstName,lastName,dob){
// this.firstName = firstName;
// this.lastName = lastName;
// this.birthday = new Date(dob);
// // this.calculateAge = function(){
// // const diff = Date.now() - this.birthday.getTime();
// // const ageDate = new Date(diff);
// // return Math.abs(ageDate.getUTCFullYear() - 1970);
// // }
// }
// Person.prototype.calculateAge = function(){
// const diff = Date.now() - this.birthday.getTime();
// const ageDate = new Date(diff);
// return Math.abs(ageDate.getUTCFullYear() - 1970);
// }
// Person.prototype.getFullName = function(){
// return `${this.firstName} ${this.lastName}`;
// }
// Person.prototype.getsMarried = function(newLastName){
// this.lastName = newLastName;
// }
// const john = new Person("John","Doe","8-12-1990");
// const mary = new Person("Mary","Johnson","March 20 1978");
// console.log(mary);
// console.log(john.calculateAge());
// console.log(mary.getFullName());
// mary.getsMarried("Smith");
// console.log(mary.getFullName());
// console.log(mary.hasOwnProperty("firstName"));
// console.log(mary.hasOwnProperty("getFullName"));
// //Prototypal Inheritance
// function Person (firstname, lastname)
// {
// this.firstname = firstname;
// this.lastname = lastname;
// }
// Person.prototype.message= function(){
// return `Hello ${this.firstname} ${this.lastname}`;
// }
// const person1 = new Person('John','Doe');
// console.log(person1.message());;
// function Customer (firstname, lastname,phone, email){
// Person.call (this,firstname,lastname);
// this.phone= phone;
// this.email = email;
// }
// Customer.prototype = Object.create(Person.prototype); //To inherit the prototype of Person
// Customer.prototype.constructor = Customer; //To make the constructor Customer instead of Person
// const customer1 = new Customer('Jack','Sparrow','100','js@gmail.com');
// console.log(customer1.message()); //Inherited from Person
// console.log(customer1); //Inherited from Customer
// console.log(Person); //Inherited from Person
// //Object.create
// const profilePrototype = {
// message: function () {
// return `Hello, I am ${this.name} my age is ${this.age}`;
// },
// setName: function (name) {
// this.name = name;
// },
// setAge: function (age) {
// this.age = age;
// },
// };
// const jack = Object.create(profilePrototype);
// jack.setName("Jack");
// jack.setAge(37);
// console.log(jack.message());
// //ES6 Classes //Syntactical Sugar over Prototypes and Inheritance
// //ES6 introduced classes. A class is a type of function, but instead of using the keyword function to initiate it, we use the keyword class , and the properties are assigned inside a constructor() method.
// class Person{
// constructor(firstName,lastName,dob){
// this.firstName = firstName;
// this.lastName = lastName;
// this.dob = new Date(dob); //Date is a built in function
// }
// message(){
// return `Hello, ${this.firstName} ${this.lastName}`;
// }
// calculateAge(){
// var diff= Date.now() - this.dob.getTime();
// var ageDate = new Date(diff);
// return Math.abs(ageDate.getUTCFullYear() - 1970);
// }
// }
// const alan = new Person("Alan","Smith","8-12-1990");
// console.log(alan.calculateAge());
// console.log(alan.message());
// console.log(alan);
// //Subclasses
// class Person{
// constructor (firstName,lastName,dob){
// this.firstName = firstName;
// this.lastName = lastName;
// this.dob = new Date(dob);
// }
// message(){
// return `Hello, ${this.firstName} ${this.lastName}`;
// }
// }
// class Customer extends Person{
// constructor(firstName,lastName,dob,phone,email){
// super(firstName,lastName,dob); //To call the constructor of the parent class
// this.phone = phone;
// this.email = email;
// }
// static getMembershipCost() //Static method is called without instantiating the class
// {
// return 500;
// }
// }
// const customer1 = new Customer("Jack","Sparrow","10-09-2002","100","email.com");
// console.log(customer1.message());
// console.log(customer1);
// console.log(Customer.getMembershipCost()); //Static method can be called without instantiating the class