2021-12-07 TIL

๋””์•„ยท2021๋…„ 12์›” 7์ผ
1

๋ฉ‹์‚ฌFE์Šค์ฟจ1๊ธฐ

๋ชฉ๋ก ๋ณด๊ธฐ
17/20
post-thumbnail

๐Ÿฆ Day28

๐Ÿ’ป ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ

๐Ÿ“ข ์ด ๊ธ€์€ ๋“œ๋ฆผ์ฝ”๋”ฉ ์—˜๋ฆฌ๋‹˜์˜ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด ํด๋ž˜์Šค ์ •๋ฆฌ ์œ ํŠœ๋ธŒ ๊ฐ•์˜ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

Class

ํด๋ž˜์Šค ์ž์ฒด์—๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ์ง€ ์•Š๊ณ  ํฐ ํ‹€, template๋งŒ ์ •ํ•ด๋†“๋Š”๋‹ค.
์–ด๋– ์–ด๋– ํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€๋งŒ ์ •์˜ํ•œ ํ›„ ์ตœ์ดˆ ํ•œ๋ฒˆ๋งŒ ์„ ์–ธํ•œ๋‹ค.
ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ์‹ค์ œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์„œ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด object ์ฆ‰, ์ธ์Šคํ„ด์Šค์ด๋‹ค.

๋ถ•์–ด๋นตํ‹€ ๋น„์œ 
Class โ†’ ๋ถ•์–ด๋นตํ‹€
object โ†’ ๋ถ•์–ด๋นต

ํด๋ž˜์Šค ๊ฐœ๋…์€ ES6์— ๋„์ž…๋˜์—ˆ๋‹ค. ES6 ์ „์—๋Š” class๊ฐ€ ์•„๋‹Œ function์„ ์ด์šฉํ•˜์—ฌ object๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

ํด๋ž˜์Šค ๋ชธ์ฒด์—๋Š” ์†์„ฑ(fields)๊ณผ ํ–‰๋™(method)์ด ์ •์˜๋œ๋‹ค. fields๋งŒ ์žˆ๋Š” ํด๋ž˜์Šค๋Š” ๋ฐ์ดํ„ฐ ํด๋ž˜์Šค๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

1. ํด๋ž˜์Šค ์„ ์–ธ

class Person{
    //constructor
    constructor(name, age) { // ์ƒ์„ฑ์ž. object๋ฅผ ๋งŒ๋“ค๋•Œ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
        //fields
        // ์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ fields์— ํ• ๋‹นํ•ด์ค€๋‹ค.
        this.name = name; 
        this.age = age;
    }

    // methods
    speak() {
        console.log(`${this.name}: hello!`);
    }
}


const tina = new Person('tina', 20);
console.log(tina.name); // tina
console.log(tina.age); // 20
tina.speak(); // tina: hello!

2. Getters์™€ Setters

class User {
    constructor(firstName, lastName, age){
        // fields - firstName, lastName, _age
        this.firstname = firstName;
        this.lastName = lastName;
        this.age = age;  // ์•„๋ž˜์— getter๊ฐ€ ์ •์˜๋˜์–ด์žˆ์œผ๋ฏ€๋กœ  this.age๊ฐ€ getter๋ฅผ ํ˜ธ์ถœ
        // ์•„๋ž˜์— setter๊ฐ€ ์ •์˜๋˜์–ด์žˆ์œผ๋ฏ€๋กœ age๋ฅผ this.age์— ํ• ๋‹นํ•  ๋•Œ setter๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค
    }

    get age() {
        return this._age;
    }

    set age(value) { // ๊ฐ’์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜จ๋‹ค
        // value๋ฅผ ์ขŒํ•ญ ๋ณ€์ˆ˜์— ํ• ๋‹นํ• ๋•Œ setter์ธ ์ž๊ธฐ์ž์‹ ์„ ๋˜ ํ˜ธ์ถœํ•˜๋ฉด์„œ call stack์ด ๊ฝ‰ ์ฐจ๊ฒŒ ๋œ๋‹ค.
        // ์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜ ์ด๋ฆ„์„ _age ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
        // value๋กœ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ this._age์— ํ• ๋‹น
        this._age = value < 0 ? 0: value;
        // ์Œ์ˆ˜ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด 0 ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ์–‘์ˆ˜ ๊ฐ’์„ ๋„ฃ์—ˆ์„๋•Œ๋Š” ๊ทธ ๊ฐ’ ๊ทธ๋Œ€๋กœ age์— ํ• ๋‹นํ•œ๋‹ค.
    }

}

const user1 = new User('Richard', 'Madden', -1); // ์‚ฌ์šฉ์ž๊ฐ€ ์‹ค์ˆ˜๋กœ ๋‚˜์ด๋ฅผ -1๋กœ ์ž…๋ ฅํ•œ ๊ฒฝ์šฐ
console.log(user1.age); // 0

3. ์ƒ์†(Inheritance)๊ณผ ๋‹คํ˜•์„ฑ(Polymorphism)

class Shape {
    constructor(width, height, color){
        this.width = width;
        this.height = height;
        this.color = color;
    }

    draw() {
        console.log(`drawing ${this.color} color!`);
    }
    
    getArea() {
        return this.width * this.height;
    }
    
}

class Rectangle extends Shape {} // Shape ํด๋ž˜์Šค์˜ fields, method๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ์ฆ‰, Shape ํด๋ž˜์Šค๊ฐ€ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.
class Triangle extends Shape { // Shape ํด๋ž˜์Šค๊ฐ€ ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ๋œ๋‹ค.
    draw() { // overriding
        super.draw(); // ๋ถ€๋ชจ class์ธ Shape์˜ draw()ํ•จ์ˆ˜ ์‚ฌ์šฉ
        console.log('๐Ÿ”บ');
    }
    getArea() { // overriding
        return (this.width * this.height) / 2;
    }
}

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());

4. instanceOf

instanceof ์—ฐ์‚ฐ์ž์˜ ์ขŒ์ธก์— ์ ๋Š” object๊ฐ€ ์šฐ์ธก์— ์ ๋Š” class์˜ instance์ธ์ง€ ํ™•์ธํ•˜์—ฌ boolean ๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

// ์œ„ ์˜ˆ์ œ์ฝ”๋“œ์—์„œ ๋งŒ๋“  ํด๋ž˜์Šค์™€ ์ธ์Šคํ„ด์Šค๋ฅผ ์˜ˆ์‹œ๋กœ ๋“ค์—ˆ์„๋•Œ
console.log(rectangle instanceof Rectangle); // true
console.log(triangle instanceof Rectangle); // false
console.log(triangle instanceof Triangle); // true
console.log(triangle instanceof Shape); // true
console.log(triangle instanceof Object); // true
                                        // ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋งŒ๋“  ๋ชจ๋“  object๋“ค์€ Object๋ฅผ ์ƒ์†ํ•œ ๊ฒƒ์ด๋‹ค


์ฐธ๊ณ ์ž๋ฃŒ
๋“œ๋ฆผ์ฝ”๋”ฉ by ์—˜๋ฆฌ | ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ 6. ํด๋ž˜์Šค์™€ ์˜ค๋ธŒ์ ํŠธ์˜ ์ฐจ์ด์ (class vs object), ๊ฐ์ฒด์ง€ํ–ฅ ์–ธ์–ด ํด๋ž˜์Šค ์ •๋ฆฌ | ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ž…๋ฌธํŽธ (JavaScript ES6)
https://www.youtube.com/watch?v=_DLhUBWsRtw

profile
์–ผ๋ ˆ๋ฒŒ๋ ˆ ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ์ž

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 12์›” 8์ผ

์˜ค ์˜ค๋Š˜์€ ๋“œ๋ฆผ์ฝ”๋”ฉ ์—˜๋ฆฌ๋‹˜ ์˜์ƒ์„ ๋ณด์…จ๊ตฐ์š”!! ์ƒ์†๊ณผ ๋‹คํ˜•์„ฑ, ๊ฒŒํ„ฐ์™€ ์„ธํ„ฐ๋Š” ์ €๋„ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ด์•ผ๊ฒ ์–ด์š”! ์ •๋ฆฌํ•ด๋†“์œผ์‹ ๊ฑฐ ๋ณด๋‹ˆ๊นŒ ๋˜ ์ƒˆ๋กญ๋„ค์š”^ใ… .. ์˜ค๋Š˜๋„ ์ˆ˜๊ณ ํ•˜์…จ์Šต๋‹ˆ๋‹ค! ๋‚ด์ผ๋„ ๊ฐ™์ด ํž˜๋‚ด๋ด์š” โ™ช(ยดโ–ฝ๏ฝ€)

1๊ฐœ์˜ ๋‹ต๊ธ€