1.1 객체지향언어: 전통적인 객체지향과는 조금 다름
// 변수에 할당 가능 -> 함수 표현식
const sum = function () {};
// 함수의 인자로 전달할 수 있어야 함
function greet(callback) {
callback();
}
greet(() => {
console.log("hello");
});
// 함수의 반환 값으로 사용할 수 있어야 함
function outer() {
return function () {
console.log("inner");
};
}
const a = outer();
a();
// 동적으로 생성 가능해야 함
const dynamicFunc = new Function("name", "return '안녕하세요, ' + name");
console.log(dynamicFunc("기수"));
3.1 class 정의 방법
3.2 class 상속 방법
3.3 정적 메서드 정의 방법
✨
https://yeonhapark.github.io/blog/javascript-class-static-method/
✔️ 인스턴스 메서드로 정의
add 메서드를 호출하려면 MathUtils 인스턴스가 필요class MathUtils {
// 인스턴스 메서드로 정의
add(n1, n2) {
return n1 + n1;
}
}
const math = new MathUtils();
const sum = math.add(10, 20);
console.log(sum);
✔️ static 으로 선언
class MathUtils {
static PI = 3.14;
// static으로 선언
static add(n1, n2) {
return n1 + n1;
}
}
const sum = MathUtils.add(10, 20);
console.log(MathUtils.PI);
function Mathss() {}
Mathss.add = function (a, b) {
return a + b;
};
console.log(Mathss.add(10, 20));
return this; 구문으로 그 객체(인스턴스) 자체를 다시 돌려줘야 함Builder 클래스를 만들어, 여러 메서드를 체이닝할 수 있도록 하세요.
class Builder {
constructor() {
this.value = ""; // 초기 문자열은 빈 문자열로 설정
}
append(text) {
this.value += text; // 전달받은 text를 value에 더해 줌
return this; // 중요: this를 통해 Builder 인스턴스 반환하여 체이닝을 가능하게 함
}
getValue() {
return this.value; // 최종 문자열을 반환함
}
}
const builder = new Builder(); // Builder 클래스의 인스턴스 생성해 builder 변수에 할당
const result = builder.append("Hello, ").append("World!").getValue();
console.log(result); // "Hello, World!"
✨체이닝 전/후
builder.append("Hello, ");
builder.append("World!");
builder.getValue();
-체이닝 후 코드
builder.append("Hello, ").append("World!").getValue();
→ 메서드 체이닝으로 작성하면 메서드를 연결해 한 줄로 쓸 수 있음
class Car {
constructor(color, speed) {
this.color = color;
this._speed = speed;
}
set speed(value) {
this._speed = value < 0 ? 0 : value;
}
get speed() {
return this._speed;
}
}
const car = new Car(200);
car.speed = -100;
car.color = "white";
console.log(car.speed);
console.log(car.color);
class Car {
constructor(color, speed) {
this.color = color;
this._speed = speed;
}
set speed(value) {
this._speed = value < 0 ? 0 : value;
}
get speed() {
return this._speed;
}
set color(value) {
this._color= value === "white" ? "black" : value;
}
get color() {
return this._color;
}
}
const car = new Car(200);
car.speed = -100;
car.color = "white";
console.log(car.speed);
console.log(car.color);
class Counter {
#count = 0;
constructor(count) {
this.#count = count;
}
increment() {
this.#count++;
}
decrement() {
this.#count--;
}
getCount() {
return this.#count;
}
}
const count = new Counter(0);
count.count = 200;
count.increment();
count.decrement();
console.log(count.getCount());
Book 클래스를 정의하고, 생성될 때마다 인스턴스 수를 카운트하도록 하세요.
class Book {
static count = 0; // static으로 선언: 클래스 자체에서 관리하는 변수
constructor(title) {
this.title = title;
Book.count++;
}
static getCount() {
return this.count; // static 메서드에서는 this로 접근할 수 있음
}
}
// 테스트 케이스
const book1 = new Book("1984");
const book2 = new Book("하이퍼리얼리티");
console.log(Book.getCount()); // 2
count가 static으로 선언되었기 때문에 인스턴스가 아닌 클래스에 속해 있음