[JavaScript] 객체지향 프로그래밍 기본 용어

|

객체 지향 프로그래밍

하나의 모델이 되는 청사진(blueprint)를 만들고,

그 청사진을 바탕으로 한 객체(object)를 만드는 프로그래밍 패턴

예제

function Car(brand, name, color) {  // Car -> class
    this.brand = brand;  // this -> avante
    this.name = name;
    this.color = color;
} 

Car.prototype.drive = function() {
    console.log(this.name + ' 가 운전을 시작합니다');
}

let avante = new Car('hyundai', 'avante', 'black');  // avante -> instance
avante.color; // 'black'
avante.drive(); // 'avante 가 운전을 시작합니다'

알아야할 용어

  1. Class

    위의 예제에서 Class는 Car 이다

  2. instance (new 키워드)

   let avante = new Car('hyundai', 'avante', 'black');
   let mini = new Car('bmw', 'mini', 'cyan');
   
   // new키워드를 써서 instance를 만들어낸다
  1. this

    new키워드로 instance를 만들어냈을때 해당 instance가 바로 this 가 된다.

    위의 예제에서 this -> avante이다.

  2. prototype

    모델의 청사진을 만들때 쓰는 원형 객체 이다.

    Car.prototype.drive = function() {
        console.log(this.name + ' 가 운전을 시작합니다');
    }
       
    Car.prototype.refuel = function() {
        console.log(this.name + ' 가 연료를 충전합니다')  
    }
       
    Car.prototype.여기에원하는걸써 = function() {
        //실행하고 싶은것을 만들면됨
    }
    
  3. **constructor(생성자) **

    // 위의 예제에서 
       
    {  
        this.brand = brand;  
        this.name = name;
        this.color = color;
    } 
       
    // 이부분이 constructor 이다.