-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.js
173 lines (119 loc) · 2.86 KB
/
class.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
'use strict';
// 1.Class declarations
class Person{
//construct
constructor(name,age){
this.name=name;
this.age=age;
}
//methods
speak(){
console.log(`${this.name}:hello!`);
}
}
const ellie =new Person('ellie' ,20);
console.log(ellie.name);
console.log(ellie.age);
ellie.speak();
// 2.Getter and setters
class User{
constructor(firstName,lastName,age){
this.firstName=firstName;
this.lastName=lastName;
this.age=age;
}
get age(){
return this._age;
}
set age(value){
// if(value<0){
// throw Error('age can not be negative');
// }
this._age=value<0?0:value;
}
}
const user1=new User('Steve','job',-1);
console.log(user1.age);
// 3.Fields
class Experiment{
publicField=2;
#privateField=0;
}
const experiment=new Experiment();
console.log(experiment.publicField);
console.log(experiment.privateField);
// 4.Static properties and methods
class Article{
static publisher='Dream Coding';
constructor(articleNumber){
this.articleNumber=articleNumber;
}
static printPulisher(){
console.log(Article.publisher);
}
}
const article1=new Article(1);
const article2=new Article(2);
console.log(article1.publisher);
console.log(Article.publisher);
Article.printPulisher();
// 5.Inheritance
class Shape{
constructor(width,height,color){
this.width=width;
this.height=height;
this.color=color;
}
draw(){
console.log(`drawing ${this.color} color of`);
}
getArea(){
return this.width*this.height;
}
}
class Rectangle extends Shape{
}
class Triangle extends Shape{
draw(){
super.draw();
console.log('&');
}
getArea(){
return this.width*this.height / 2;
}
toString(){
return `Triangle: color : ${this.color}`;
}
}
const rectangle=new Rectangle(20,20,'blue');
rectangle.draw();
console.log(rectangle.getArea());
const triangle=new Triangle(20,20,'red');
triangle.draw();
console.log(triangle.getArea());
// 6.Class checking: instance0f
console.log(rectangle instanceof Rectangle);
console.log(triangle instanceof Rectangle);
console.log(triangle instanceof Triangle);
console.log(triangle instanceof Shape);
console.log(triangle instanceof Object);
console.log(triangle.toString());
// object 는 모든걸 상속한거
// https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference
function calculate(command,a,b){
switch(command){
case 'add':
return a+b;
case 'substract':
return a-b;
case 'divide':
return a/b;
case 'multiply':
return a*b;
case 'remainder':
return a%b;
default:
throw Error('unkonwn command');
}
}
console.log(calculate('add',2,3));