-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript_oops.js
More file actions
129 lines (97 loc) · 2.83 KB
/
javascript_oops.js
File metadata and controls
129 lines (97 loc) · 2.83 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
// classes and objects
class Person {
#name = 'hello'
#age
//making constructor
constructor(name,age) {
this.#name = name;
this.#age = age ;
}
introduceSelf() {
// console.log();
return `Hi! I'm ${this.name} and i am ${this.age} years old`
}
}
class Profeisonal extends Person{
constructor(name,job,age){
super(name,age);
this.job = job ;
}
// method overriden property
introduceSelf(){
// console.log("he is the professional guy");
return super.introduceSelf() + ` and working as ${this.job}`;
// console.log(`Hi! I'm ${this.name} and i am ${this.age} years old and working as ${this.job}` )
}
}
// const p1 = new Person('jayendra');
// console.log(p1.name)
// const p2 = new Profeisonal('harshad','full-stack development',21);
// console.log(p2.introduceSelf());
class Shape{
#name
#sidesLength
#sides
// constructor(){
// console.log("this difualt constructor")
// }
constructor(name,sides,sidesLength){
this.#name = name ;
this.#sides = sides ;
this.#sidesLength = JSON.parse(JSON.stringify(sidesLength)); //for deep_copy
}
perimeter(){
return this.#sidesLength.reduce((acc,e) => acc + e);
}
area(){
}
getSides(){
return this.#sides;
}
getSideLegth(){
return this.#sidesLength
}
}
class Square extends Shape{
constructor(name,sides,sidesLength){
// super(name,sides,sidesLength)
super()
try{
// console.log(this.getSide())
if(this.getSides() != 4)
throw new "must be contain four sides";
const s = this.getSideLegth()[0];
// console.log(this.getSideLegth());
const sq = this.getSideLegth().filter((e) => s === e);
if(sq.length != this.getSides()){
// throw new Error("this is not valid square");
console.log(sq.length);
}
}
catch(err){
console.log(err);
}
}
}
const sq = new Square('square',4,[2,2,2,2])
// const square = new Shape('square',[2,2,2,2]);
// // console.log(square.sidesLength);
// console.log(square.perimeter())
// Error handle try-catch
// here not handle the error due to callback which execute after timer out after that
// its get syntax error but not handle the error by try-catch
// try {
// setTimeout(function() {
// noSuchVariable; // script will die here
// }, 5000);
// } catch (err) {
// alert( "won't work" );
// }
// handle by the catch
// setTimeout(function() {
// try {
// noSuchVariable; // try...catch handles the error!
// } catch {
// alert( "error is caught here!" );
// }
// }, 1000);