๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“’ JavaScript

[JavaScript] Class ์™€ ์ƒ์†

by Tamii 2021. 3. 10.
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ class

๊ด€๋ จ ์žˆ๋Š” ํ•จ์ˆ˜์™€ ๋ณ€์ˆ˜๋ฅผ ๋ฌถ์–ด๋†“์€ ๊ฒƒ 

์ƒ์†๊ณผ ๋‹ค์–‘์„ฑ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์Œ 

 

 - fields

 - methods

 

* data class

  : methods๊ฐ€  ์—†๋Š” fields( data)๋งŒ ์žˆ๋Š” class

 

 

class object 

- template ํ…œํ”Œ๋ฆฟ 

- declare once

- no data in (๋ฐ์ดํ„ฐ๋Š” ์—†๊ณ  ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํ‹€)

 class ๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“  ๊ฒƒ = ๊ฐ์ฒด

 

 

javascript ์˜ class

javascript ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ์–ธ์–ด

object๊ฐ€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋ฉฐ ๋Œ์•„๊ฐ€๋Š” application

 

- ECMA Script 6๋ฒ„์ „์—์„œ ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ 

- ES6 ์—์„œ ์ƒ๊น€

- class๊ฐ€ ์žˆ๊ธฐ ์ „์—๋Š”  constructor function์„ ์ด์šฉํ•ด template์„ ๋งŒ๋“ค๊ณ  ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ–ˆ์—ˆ๋‹ค.

- ๊ธฐ์กด์— ์กด์žฌํ•˜๋˜ prototype-based ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ฌธ๋ฒ•๋งŒ class๊ฐ€ ์ถ”๊ฐ€

 

 

 


๐Ÿ“Œ Constructor function  (์ƒ์„ฑ์ž ํ•จ์ˆ˜) ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ

 - ๊ฐ์ฒด ์ƒ์„ฑ

 - ๊ฐ์ฒด์˜ ์ดˆ๊ธฐ ์ƒํƒœ ์„ธํŒ… 

 

 

function Person(name, first, second, third) {
    this.name= name;
    this.first = first
    this.second = second;
}


var kim = new Person('kim',10,20);

๊ธฐ์กด์˜ javascript ๋Š” ์ƒ์„ฑ์žํ•จ์ˆ˜(Person)์„ new๋กœ ๋งŒ๋“ค์–ด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค.

 -> Person์ด๋ผ๋Š” ํ•จ์ˆ˜๋Š” ์ƒ์„ฑ์ž๊ฐ€ ๋จ

 

 

 

 

๐Ÿ“Œ Class ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด ์ƒ์„ฑ

constructor()

๊ฐ์ฒด ์ƒ์„ฑ ๋  ๋•Œ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐ์ƒํƒœ๋ฅผ ์ง€์ •

๊ฐ์ฒด๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ธฐ ์ง์ „์— ์‹คํ–‰๋  ํ•จ์ˆ˜ 

class Person {
    constructor(name,first,second){

        this.name= name;
        this.first = first;
        this.second = second;
    }
 }
 
 var kim = new Person('kim',10,20);

 

 

 

 

๋ฉ”์„œ๋“œ ์ƒ์„ฑ

1) ๋‚ด๋ถ€์— ์ƒ์„ฑ

class Person {
    constructor(name,first,second){

        this.name= name;
        this.first = first;
        this.second = second;
    }
    sum(){
        return this.first + this.second
    }
}

var kim = new Person('kim',10,20);

2) ์™ธ๋ถ€์— ์ƒ์„ฑ 

class Person {
    constructor(name,first,second){

        this.name= name;
        this.first = first;
        this.second = second;
    }
}

//Person์ด๋ผ๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜์˜ ๋ชจ๋“  ๊ฐ์ฒด์—์„œ ์‚ฌ์šฉํ•  ๋ฉ”์„œ๋“œ ์ƒ์„ฑ
Person.prototype.sum = function(){
    return 'prototype' + (this.first + this.second);
}

var kim = new Person('kim',10,20);

 

 

 

 

๋งŒ์•ฝ kim์˜ ๊ฐ์ฒด๊ฐ€  ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค๊ณผ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ sum ์„ ๊ฐ€์ง€๊ณ  ์‹ถ๋‹ค๋ฉด ,

kim.sum = function(){
    return 'this'+(this.first + this.second);
}

์ด code๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ํ•ญ์ƒ ์‹คํ–‰ ์‹œ  ๋‹ค์Œ์˜ ์šฐ์„ ์ˆœ์œ„๋กœ ์‹คํ–‰ํ•จ

1) ๊ฐ์ฒด ๋ณธ์ธ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฉ”์„œ๋“œ 

  : kim.sum ํ™•์ธ

2) ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž์˜ Prototype์˜ ๋ฉ”์„œ๋“œ 

  : Person.prototype

 

 

 


๐Ÿ“Œ ์ƒ์†

์ƒ์†์€ ๋ถ€๋ชจ code ์˜ ๊ธฐ๋Šฅ์„ ์ด์–ด๋ฐ›์•„ ๊ณต์œ ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ 

๊ณ„์† ๋ฐ˜๋ณต๋˜์–ด์ง€๋Š” ๊ฒƒ์„ 1๋ฒˆ๋งŒ ์ •์˜ํ•˜์—ฌ ์ƒ์†์‹œ์ผœ  ์žฌ์‚ฌ์šฉํ•จ 

 

 

ํ•„์š”ํ•œ ๊ฒฝ์šฐ

 - ํƒ€์ธ์ด ๋งŒ๋“  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, code๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๊ฒฝ์šฐ

 - ํ•ด๋‹น class๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ( ์ˆ˜์ • ์‹œ ์—…๋ฐ์ดํŠธ ๋ถˆ๊ฐ€ ๋“ฑ์˜ ์ด์œ ๋กœ)

 - ๋ชจ๋“  ๊ณณ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ์— ์‚ฌ์šฉํšŸ์ˆ˜๊ฐ€ ์ ์€ ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ 

 

์žฅ์ 

์ค‘๋ณต๋˜๋Š” code ์ œ๊ฑฐ 

๋™์‹œ ๋‹ค๋ฐœ์ ์ธ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•ด ์œ ์ง€๋ณด์ˆ˜์˜ ํŽธ์˜์„ฑ ๋„๋ชจ

 

 

โ–บ Class ์—์„œ์˜ ์ƒ์† 

class Shape { 
    constructor(width, height, color) {
        this.width = width;
        this.height =height;
        this.color = color;
    }
    
    draw () {
        console.log(`drawing ${this.color} color of`);
    }

    getArea() {
        return width * this.height;
    }
}



class Rectangle extends Shape{ }

class Triangle extends Shape{
    draw(){
        console.log('โ–ฒ')
    }
    //overriding
    getArea(){
        return (this.width * this.height)/2;
    }
}

const rectangle = new Rectangle(20,20,'blue');
rectangle.draw();
const triangle = new Triangle(20,20,'red');
triangle.draw();

Shape : ๋ชจ์–‘์„ ๋งŒ๋“œ๋Š” class

Triangle๋Š” Shape์„ ์ƒ์†๋ฐ›์Œ์œผ๋กœ์จ draw, getArea() ๋ณ€์ˆ˜์™€ ๋ฉ”์„œ๋“œ๋ฅผ  ์žฌ์ •์˜ํ•  ํ•„์š” ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ,

draw() overriding ์„ ํ†ตํ•ด draw() ๋ฉ”์„œ๋“œ ์žฌ์ •์˜ 

 

 

 

โ– instanceof 

object ๊ฐ€ class์˜ instance์ธ์ง€ ์•„๋‹Œ์ง€ ํ™•์ธ 

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

 

 

 

 

super

๋ถ€๋ชจ class๋ฅผ ๋ถˆ๋Ÿฌ์„œ ์ผ์„ ์‹œํ‚ค๊ณ , ๋ถ€๋ชจ๊ฐ€ ํ•˜์ง€ ๋ชปํ•˜๋Š” ์ผ์„ ๋‚˜๋งŒ ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ 

๋ถ€๋ชจ๊ฐ€ ๊ฐ€์ง„ ๊ธฐ๋Šฅ์„ ์‹คํ–‰ + ์ž์‹์—์„œ๋งŒ ๊ตฌํ˜„ํ•  ๊ธฐ๋Šฅ๊ณผ ์—ฎ์–ด์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅ 

 

 

 

โ– ์ž์‹ class (PersonPlus) ์— ์ธ์ž๋ฅผ ๋”ํ•ด์ฃผ๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ

PersonPlus ์—  constructor ์„ ์ „๋ถ€ ๋‹ค์‹œ ์“ฐ๋Š”๊ฑด ๋น„ํšจ์œจ 

 

<๋ถ€๋ชจ>

class Person {
    constructor(name,first,second){

        this.name= name;
        this.first = first;
        this.second = second;
    }
    sum(){
        return this.first + this.second;
    }
}

 

 

<์ž์‹>

class PersonPlus extends Person{

    average(){
        return (this.first+this.second)/2;
    }
}

class PersonPlus extends Person{

    constructor(name,first,second,third){
        super(name,first,second,third)
        this.third = third;
    }
    
    sum(){
        return super.sum()+this.third;
    }
    
    
    average(){
        return (this.first+this.second+this.third)/2;
    }
}



var kim = new PersonPlus('kim',10,20,30);
console.log('kim',kim.average());

์ž์‹ class(PersonPlus)์—์„œ ๋ถ€๋ชจ class(Person) ์˜ name,first,second ์„ return ๋ฐ›์•„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ 

 

 

 

 

 

 

 

 

 

๐Ÿง ์ƒ๊ฐํ•ด๋ณผ ๊ฒƒ 

 

์ƒ์†์€ ๋ฌด์—‡์ธ๊ฐ€?

์ƒ์†์€ ๋ถ€๋ชจ code ์˜ ๊ธฐ๋Šฅ์„ ์ด์–ด๋ฐ›์•„ ๊ณต์œ ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ 

 

์ƒ์†์ด ์—†์œผ๋ฉด ๋ฌด์—‡์ด ๋ถˆํŽธํ•œ์ง€?

์ƒ์†์ด ์—†์œผ๋ฉด ํ•„์š”ํ•œ ๋ฉ”์„œ๋“œ๋“ค์„ ์ค‘๋ณต์œผ๋กœ  ๊ตฌํ˜„ํ•ด ๊ธธ์ด๊ฐ€ ๊ธธ์–ด์ง„๋‹ค.

์ƒ์†์ด ์—†์œผ๋ฉด ์ผ๋ถ€๋ถ„ ๊ธฐ๋Šฅ ๋ณ€๊ฒฝ ์‹œ์—๋„ ํด๋ž˜์Šค ๋‚ด ์ฝ”๋“œ๋ฅผ ๋ณ€๊ฒฝํ•ด  ํ›„์— ์˜ค๋ฅ˜๊ฐ€ ๋‚  ์ˆ˜๋„ ์žˆ๊ณ ,

ํ˜น์€ (๊ธฐ๋Šฅ์ด ๋“ค์–ด๊ฐ„ )ํด๋ž˜์Šค๋“ค ์ „๋ถ€๋ฅผ ๋ณ€๊ฒฝํ•ด์•ผ ํ•ด ์œ ์ง€๋ณด์ˆ˜์— ์ข‹์ง€ ์•Š๋‹ค.

 


 

<์˜ˆ์‹œ๋กœ ๊ณต๋ถ€ํ•˜๊ธฐ  > 

 

class ์žํŒ๊ธฐ {

  int  numberOfCoffee;

  puTcoins(){}

  makeCofffe(){}
  
}

โ– getter , setter ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  

numberOfCoffee<0์ด ๋  ์ˆ˜ ์—†์Œ 

์‚ฌ์šฉ์ž๊ฐ€ -1๊ฐ™์€๊ฑธ๋กœ ์ง€์ •ํ•˜๋ฉด setter ๋กœ 0์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ ์ฃผ๋Š” ๊ฒƒ

 

โ– private ์“ฐ๋Š” ์ด์œ  

์‚ฌ์šฉ์ž๊ฐ€ ๋งˆ์Œ๋Œ€๋กœ ์„ค์ •ํ•˜๋ฉด ์•ˆ๋˜๋‹ˆ private numberOfCoffee๋กœ ์ง€์ • => encapsulate

 

 

 

getter & setter

class User {
    constructor(firstName, lastName,age) {
        this.firstName = firstName;
        this.lastName= lastName;
        this.age = age;
    }
    //getter
    get age() {
        return this._age;
    }
    //setter
    set age(value) {
        // if(value<0){
        //     throw Error ('age can not be negetive');
        // }
        this._age = value<0? 0:value;
    }

}

const user1 = new User('Steve','Job',-1);
console.log(user1.age);

getter setter  : _ ๋ณ€์ˆ˜ (์–ธ๋”๋ฐ”) 

ํ˜ธ์ถœ ์‹œ์—๋Š” .๋ณ€์ˆ˜ ๋กœ ํ˜ธ์ถœ ๊ฐ€๋Šฅ 

 

getter ์ •์˜ ์ˆœ๊ฐ„ ( get age() ) 

this.age = age

this.age

  => ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ผ์˜จ  age ๊ฐ€์ ธ์˜ค๊ธฐ X

  => get age() getter ํ˜ธ์ถœ O

 

setter ์ •์˜ ์ˆœ๊ฐ„ 

this.age = age

 => ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ฐ’ ํ• ๋‹น X

 => setter ํ˜ธ์ถœ 

 

 

 

 

Field ( public, private) (์•„์ง ์ตœ์‹  ์—…๋ฐ์ดํŠธ ์•ˆ๋จ)

์ƒ์„ฑ์‚ฌ๋ฅผ ์“ฐ์ง€ ์•Š๊ณ  field ์ •์˜ ๊ฐ€๋Šฅ 

class Temp {

	publicField = 2;
    #privateField = 0;
}

const temp = new Temp();
console.log(Temp.publicField); //2
console.log(Temp.privateField); //undefined

 

 

static

object ์™€ ๊ด€๊ณ„์—†์ด class ์ž์ฒด์— ์—ฐ๊ฒฐ 

๋“ค์–ด์˜ค๋Š” data ์— ์ƒ๊ด€์—†์ด ๊ณตํ†ต์ ์œผ๋กœ class์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์—์„œ ๋ฉ”๋ชจ๋ฆฌ์˜ ์‚ฌ์šฉ์„ ์ค„์ผ ์ˆ˜ ์žˆ์Œ

class Article {
    static publisher = 'Tami world';
    constructor (num){
        this.num= num;
    }
    static printPublicher() {
        console.log(Article.publisher);
    }
}

const article1 = new Article(1);
console.log(article1.publisher); //undefined
console.log(Article.publisher); // Tami world

Article.publisher ์ฒ˜๋Ÿผ 

class  ์ž์ฒด๋กœ ๋ถˆ๋Ÿฌ์ค˜์•ผ ํ•จ

 

 

 


 

toString() ? 

object class ๋“ค์€ javascript ์˜ object๋ฅผ ์ƒ์†ํ•œ ๊ฒƒ 

constructor, toString()....

์–ด๋–ค object๋“ฑ ๊ณตํ†ต ๋ฉ”์„œ๋“œ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ 

console.log(triangle.toString()); // [Object, Object] 

class Triangle extends Shape{

    toString() {
        return `Triangle color : ${this.color}`;
    }
}

console.log(triangle.toString()); // Triangle color : red

 

 

 

 

 

 

 

์œ ์šฉํ•œ ์‚ฌ์ดํŠธ 

 

BABEL

ํ˜„์žฌ ์ง  code๋ฅผ ์˜ˆ์ „ ๋ฒ„์ „์˜ javascript ๋กœ  ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ์ปดํŒŒ์ผ๋Ÿฌ&ํŠธ๋žœ์ŠคํŒŒ์ผ๋Ÿฌ

์ž‘์„ฑํ•œ code๋ฅผ ๊ณผ๊ฑฐ์˜ code๋กœ converting  => ์‹ค์ œ ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž‘๋™ํ•˜๋„๋ก

 

babeljs.io/

 

Babel · The compiler for next generation JavaScript

The compiler for next generation JavaScript

babeljs.io

 

 

 

์ฐธ๊ณ ์˜์ƒ

www.youtube.com/watch?v=_DLhUBWsRtw

 

 

 

๋Œ“๊ธ€