-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassesAndObjects.js
More file actions
95 lines (88 loc) · 1.91 KB
/
Copy pathclassesAndObjects.js
File metadata and controls
95 lines (88 loc) · 1.91 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
const employee={
calcTax1(){
console.log("tax is 10%")
}
};
let Vijay={
salary:()=>{
console.log("salary : 10000");
},
calcTax1(){
console.log("tax is 20%")
}
};
Vijay.__proto__=employee;//ASSIGNING THE CALCTAX AS PROTOTYPE
Vijay.calcTax1();//if same name calctax1 are defined in prototype and obj,the obj function overrides the prototype
class Car{ //class
start(){
console.log("start");
}
stop(){
console.log("stop")
}
setBrand(brand){
this.brand=brand; //same name can be given
}
}
let obj1=new Car();
obj1.start();
obj1.stop();
obj1.setBrand('object1');
//classes used when same template is used again and again
//Constructor
class student{
constructor(){
console.log('creating new object');
}
name(Name){
console.log(`i am ${Name}`);
}
}
//creating an object for student named vijay
let vijay=new student();//constructor invoked
vijay.name('Vijay');
//another student
let vinay=new student();//new constructor
vinay.name('Vinay');
//Inheritance:Passing down prop & methods from parent to child class
//Method Overriding(use extends keyword)
class Parent{
hello(){
console.log("hello");
}
}
class Child extends Parent{
}
let obj=new Child();
obj.hello();
//in case of same method in both parent and child class,child class overrides it.
//to acess parent class properties from child class, we use the super keyword
class Human{
eats(){
console.log('eat');
}
sleeps(){
console.log('sleep');
}
work(){
console.log('do nothing');
}
}
class Doctor extends Human{
work(){
super.work() //invoke super
console.log('see patients and cure them');
}
}
let dochuman=new Doctor();
dochuman.work();
//error handling
try{
console.log(a+b+c);
}
catch(err){
console.log(err)
}
finally {
console.log("i am finally block")
}