-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript18.js
89 lines (77 loc) · 2 KB
/
script18.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
// class test{
// constructor(height,width){
// this.width=width;
// this.height=height
// }
// set width(newwidth){
// if(newwidth > 0){
// this._width=newwidth;
// }
// else
// console.error(`please enter a positive width`);
// }
// set height(newheight){
// if(newheight > 0){
// this._height=newheight;
// }
// else
// console.error( `please enter a valid height`)
// }
// get height(){
// return this._height;
// }
// get width(){
// return this._width;
// }
// get area(){
// return this._width*this._height;
// }
// }
// const a = new test(100,12);
// console.log(a.height,a.width);
// exmaple 2
class person{
constructor(firstname,lastname,age){
this.firstname=firstname
this.lastname=lastname
this.age=age
}
set firstname(newfirstname){
if(newfirstname.length>0 && typeof newfirstname === "string"){
this._firstname=newfirstname
}
else{
console.error(`give arguements correctly`)
}
}
set lastname(newlastname){
if(newlastname.length > 0 && typeof newlastname === "string"){
this._lastname=newlastname
}
else{
console.error(`give the proper arguments ie correct value`);
}
}
set age(newage){
if(newage>0 && typeof newage === "number"){
this._age=newage;
}
else{
console.error(`age must be a non negative number`)
}
}
get age(){
return this._age;
}
get lastname(){
return this._lastname;
}
get firstname(){
return this._firstname;
}
get fullname(){
return this._firstname + " " + this._lastname
}
}
const p = new person('vatsal','verma',12)
console.log(p.firstname,p.lastname,p.age,p.fullname)