10일차 부트캠프

 객체 지향

function Car(brand, name, color){

this.brand = brand;

this.name = name;

this.color = color;

}


class Car {

constructor(brand, name, color){

this.brand = brand;

this.name = name;

this.color = color;

}

}


두방법은 동일하나 밑의 예시는 ES5에서는 불가능하다.


let avante = new Car('hyun', 'avante', 'black');

이런식으로 새로운 인스턴스를 만들어 낼 수 있다.


메소드를 클래스 안에서 정의 할 수 있는데


function Car(brand, name, color){

}


Car.prototype.refuel = function() {

...

}


class Car {

constructor(brand, name, color){

}

refuel() {

}

}


클래스를 이런식으로 정의를 했다면 우리는


class Car {

constructor(brand, name, color){

this.brand = brand;

this.name = name;

this.color = color;

}

refuel() {

console.log(this.name + '가 연료를 넣습니다')

}

}

이런 클래스를 만들 수있다. 이것을 밖에서 사용하기 위해


let avante = new Car('hyun', 'avante', 'black');

avante.color; //'black'

avante.refuel(); // 'avante가 연료를 넣습니다'

댓글

이 블로그의 인기 게시물

[C#] 다른 파일에 있는 클래스 불러오기

[C#] 콘솔 데이터 저장 및 불러오기

[C#] Array 활용법