λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
Language/Javascript

[Javascript]μžλ°”μŠ€ν¬λ¦½νŠΈ 클래슀 μ΄ν•΄ν•˜κΈ°

by YJ Dev 2024. 6. 25.
728x90
λ°˜μ‘ν˜•
SMALL

μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ ν΄λž˜μŠ€λŠ” 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°μ„ κ΅¬ν˜„ν•˜λŠ” 데 맀우 μœ μš©ν•œ λ„κ΅¬μž…λ‹ˆλ‹€. ν΄λž˜μŠ€λŠ” 객체의 ꡬ쑰와 행동을 μ •μ˜ν•˜λŠ” 틀을 μ œκ³΅ν•˜λ©°, 이λ₯Ό 톡해 더 κΉ”λ”ν•˜κ³  κ΄€λ¦¬ν•˜κΈ° μ‰¬μš΄ μ½”λ“œλ₯Ό μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 이번 ν¬μŠ€νŒ…μ—μ„œλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈ 클래슀의 κΈ°λ³Έ κ°œλ…κ³Ό 예제λ₯Ό 톡해 클래슀λ₯Ό ν™œμš©ν•˜λŠ” 방법을 μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€.

μžλ°”μŠ€ν¬λ¦½νŠΈ 클래슀


01. 클래슀 μ •μ˜ν•˜κΈ°: κΈ°λ³Έ ꡬ쑰와 ν‚€μ›Œλ“œπŸ·οΈ

μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ ν΄λž˜μŠ€λŠ” 객체 지ν–₯ ν”„λ‘œκ·Έλž˜λ°(OOP)을 μ§€μ›ν•˜κΈ° μœ„ν•΄ ES6(ECMAScript 2015)μ—μ„œ λ„μž…λœ λ¬Έλ²•μž…λ‹ˆλ‹€. 클래슀λ₯Ό μ‚¬μš©ν•˜λ©΄ 객체λ₯Ό μƒμ„±ν•˜κΈ° μœ„ν•œ ν…œν”Œλ¦Ώμ„ μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€. ν΄λž˜μŠ€λŠ” ν”„λ‘œνΌν‹°(속성)와 λ©”μ„œλ“œ(κΈ°λŠ₯)둜 κ΅¬μ„±λ©λ‹ˆλ‹€. λ‹€μŒμ€ μžλ°”μŠ€ν¬λ¦½νŠΈ 클래슀의 κΈ°λ³Έ κ°œλ…κ³Ό μ˜ˆμ œμž…λ‹ˆλ‹€.

클래슀 μ •μ˜

μžλ°”μŠ€ν¬λ¦½νŠΈ 클래슀λ₯Ό μ •μ˜ν•˜λ €λ©΄ class ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€.

class Person {
    // μƒμ„±μž λ©”μ„œλ“œ: 객체 생성 μ‹œ ν˜ΈμΆœλ©λ‹ˆλ‹€.
    constructor(name, age) {
        this.name = name; // ν”„λ‘œνΌν‹° μ •μ˜
        this.age = age;   // ν”„λ‘œνΌν‹° μ •μ˜
    }

    // λ©”μ„œλ“œ μ •μ˜
    sayHello() {
        console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
    }
}

객체 생성

new ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ ν΄λž˜μŠ€λ‘œλΆ€ν„° 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

const person1 = new Person('Alice', 30);
const person2 = new Person('Bob', 25);

person1.sayHello(); // 좜λ ₯: Hello, my name is Alice and I am 30 years old.
person2.sayHello(); // 좜λ ₯: Hello, my name is Bob and I am 25 years old.

클래슀의 ꡬ성 μš”μ†Œ

μƒμ„±μž(constructor) λ©”μ„œλ“œ

  • constructor λ©”μ„œλ“œλŠ” 객체가 생성될 λ•Œ ν˜ΈμΆœλ©λ‹ˆλ‹€.
  • 이 λ©”μ„œλ“œλ₯Ό 톡해 초기 ν”„λ‘œνΌν‹°λ₯Ό μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν”„λ‘œνΌν‹°(properties)

  • 클래슀 λ‚΄λΆ€μ—μ„œ μ •μ˜λœ λ³€μˆ˜λ‘œ, 객체의 속성을 λ‚˜νƒ€λƒ…λ‹ˆλ‹€.
  • μƒμ„±μž λ‚΄λΆ€μ—μ„œ this ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ •μ˜ν•©λ‹ˆλ‹€.

λ©”μ„œλ“œ(methods)

  • 클래슀 λ‚΄λΆ€μ—μ„œ μ •μ˜λœ ν•¨μˆ˜λ‘œ, 객체의 λ™μž‘μ„ λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

02. μƒμ„±μž - 객체 μ΄ˆκΈ°ν™”ν•˜κΈ°πŸ› οΈ

클래슀 μƒμ„±μžλŠ” 객체가 생성될 λ•Œ ν˜ΈμΆœλ˜λŠ” νŠΉλ³„ν•œ λ©”μ„œλ“œμž…λ‹ˆλ‹€. μƒμ„±μž μ•ˆμ—μ„œ 객체의 초기 μƒνƒœλ₯Ό μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

class Animal {
    constructor(name, species, age) {
        this.name = name; // 동물 이름
        this.species = species; // 동물 μ’…
        this.age = age; // 동물 λ‚˜μ΄
    }
}

03. λ‹€μ–‘ν•œ 객체 μƒμ„±ν•˜κΈ°πŸ§ͺ

μœ„μ—μ„œ μ •μ˜ν•œ Animal 클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ μ—¬λŸ¬ 객체λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

const animal1 = new Animal('Leo', 'Lion', 5);
const animal2 = new Animal('Bella', 'Elephant', 8);
const animal3 = new Animal('Max', 'Dog', 3);

console.log(animal1);
console.log(animal2);
console.log(animal3);

각 μΈμŠ€ν„΄μŠ€λŠ” Animal 클래슀의 속성과 λ©”μ„œλ“œλ₯Ό μƒμ†λ°›μŠ΅λ‹ˆλ‹€. μœ„μ˜ μ½”λ“œλŠ” 각각의 동물 객체λ₯Ό μƒμ„±ν•˜κ³  μ½˜μ†”μ— 좜λ ₯ν•©λ‹ˆλ‹€.


04. 클래슀 속성에 μ ‘κ·Όν•˜κΈ°πŸŽ¨

μƒμ„±λœ 객체의 속성에 μ ‘κ·Όν•˜λ €λ©΄ 점 ν‘œκΈ°λ²•μ„ μ‚¬μš©ν•©λ‹ˆλ‹€.

console.log(animal1.name); // 좜λ ₯: Leo
console.log(animal2.species); // 좜λ ₯: Elephant
console.log(animal3.age); // 좜λ ₯: 3

05. 클래슀 λ©”μ„œλ“œ ν˜ΈμΆœν•˜κΈ°πŸ—£οΈ

클래슀 λ©”μ„œλ“œλŠ” 객체의 λ™μž‘μ„ μ •μ˜ν•©λ‹ˆλ‹€. 이λ₯Ό ν˜ΈμΆœν•˜μ—¬ νŠΉμ • μž‘μ—…μ„ μˆ˜ν–‰ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

class Animal {
    constructor(name, species, age) {
        this.name = name;
        this.species = species;
        this.age = age;
    }

    displayInfo() {
        console.log(`Animal: ${this.name} (${this.species}), Age: ${this.age}`);
    }
}

const animal1 = new Animal('Leo', 'Lion', 5);
animal1.displayInfo(); // 좜λ ₯: Animal: Leo (Lion), Age: 5

06. ν΄λž˜μŠ€μ™€ μΈμŠ€ν„΄μŠ€μ˜ νƒ€μž… ν™•μΈν•˜κΈ°

ν΄λž˜μŠ€μ™€ μΈμŠ€ν„΄μŠ€μ˜ νƒ€μž…μ„ ν™•μΈν•˜λ €λ©΄ instanceof μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. 이λ₯Ό 톡해 객체가 νŠΉμ • 클래슀의 μΈμŠ€ν„΄μŠ€μΈμ§€ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ ν΄λž˜μŠ€λŠ” ν•¨μˆ˜λ‘œ μ·¨κΈ‰λ˜λ©°, 클래슀둜 μƒμ„±ν•œ μΈμŠ€ν„΄μŠ€λŠ” 객체둜 μ·¨κΈ‰λ©λ‹ˆλ‹€.

// instanceof μ—°μ‚°μž μ‚¬μš©
console.log(animal1 instanceof Animal); // 좜λ ₯: true
console.log(animal2 instanceof Animal); // 좜λ ₯: true
console.log(animal3 instanceof Animal); // 좜λ ₯: true

console.log(animal1 instanceof Array); // 좜λ ₯: false

// typeof μ—°μ‚°μž μ‚¬μš©
console.log(typeof Animal); // "function
console.log(typeof animal1); // 좜λ ₯: object
console.log(typeof animal2); // 좜λ ₯: object
console.log(typeof animal3); // 좜λ ₯: object

07. 핡심 λ‚΄μš©πŸ‘€

μžλ°”μŠ€ν¬λ¦½νŠΈ 클래슀

728x90
λ°˜μ‘ν˜•
LIST