-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_9_2_11.html
41 lines (36 loc) · 1.09 KB
/
js_9_2_11.html
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
<!-- static 키워드 사용하기 -->
<script>
class Square {
#length
static #counter = 0
static get counter() {
return Square.#counter
}
constructor(length) {
this.length = length
Square.#counter += 1
}
static perimeterOf(length) {
return length << 2
}
static areaOf(length) {
return length ** 2
}
get length() { return this.#length }
get perimeter() { return this.#length << 2 }
get area() { return this.#length ** 2 }
set length(length) {
if (length <= 0)
throw '길이는 0보다 커야 합니다.'
this.#length = length
}
}
// static 속성 사용하기
const squareA=new Square(10)
const squareB=new Square(20)
const squareC=new Square(30)
console.log(`지금까지 생성된 Square 인스턴스는 ${Square.counter}개 입니다.`)
// static 메소드 사용하기
console.log(`한 변의 길이가 20인 정사각형의 둘레는 ${Square.perimeterOf(20)}입니다.`)
console.log(`한 변의 길이가 30인 정사각형의 넓이는 ${Square.areaOf(30)}입니다.`)
</script>