2025-01-26T02:57:06
This commit is contained in:
@@ -162,4 +162,45 @@ animals.forEach(animal => animal.speak());
|
||||
// 동물이 소리를 냅니다.
|
||||
```
|
||||
|
||||
#### 정적 필드 / 메서드
|
||||
정적 필드는 클래스 자체에 속하는 필드로, 인스턴스를 생성하지 않고도 클래스 이름으로 직접 접근할 수 있는 필드입니다.
|
||||
```javascript
|
||||
class MathUtils {
|
||||
static PI = 3.14159;
|
||||
|
||||
static square(x) {
|
||||
return x * x;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(MathUtils.PI); // 3.14159
|
||||
console.log(MathUtils.square(5)); // 25
|
||||
```
|
||||
|
||||
#### Getter / Setter
|
||||
```javascript
|
||||
class Person {
|
||||
#name; // private 속성
|
||||
#age;
|
||||
|
||||
constructor(name, age) {
|
||||
this.#name = name;
|
||||
this.#age = age;
|
||||
}
|
||||
|
||||
get name() {
|
||||
return this.#name;
|
||||
}
|
||||
|
||||
set name(value) {
|
||||
this.#name = value;
|
||||
}
|
||||
}
|
||||
|
||||
const person1 = new Person('홍길동', 30);
|
||||
console.log(person1.name); // 홍길동 (getter를 통해 접근)
|
||||
console.log(person1.#name); // Error: Property '#name' is private
|
||||
```
|
||||
* get과 set을 사용하여 속성에 대한 접근을 제어할 수 있습니다.
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user