👉🏽자바스크립트의 '객체'와 구분하기
: 자바스크립트의 (그냥) 객체와, 클래스를 바탕으로 한 인스턴스 객체를 구분해서 쓸 것!
function Car(brand, name, color) { // 인스턴스가 만들어질 때 실행되는 코드 }
class Car { constructor(brand, name, color) { // 인스턴스가 만들어질 때 실행되는 코드 } }
let avante = new Car('hyundai', 'avante', 'black'); let mini = new Car('bmw', 'mini', 'white'); let beetles = new Car('volkswagen', 'beetles', 'red');
'hyundai'
, 'avante'
, 'black'
..)은 각각 인스턴스의 어떤 속성(brand
, name
, color
) 혹의 메서드의 값을 지정하는데 이용됨. ES5
function Car(brand, name, color) { // 🌟 괄호 안은 그냥 클래스의 매개변수들(아래의 this.XX 형태의 속성과 구분할 것) this.brand = brand; this.name = name; this.color = color; }
ES6
class Car { constructor(brand, name, color) { // 🌟 괄호 안은 그냥 클래스의 매개변수들(아래의 this.XX 형태의 속성과 구분할 것) this.brand = brand; this.name = name; this.color = color; } }
ES5
function Car(brand, name, color) {---생략---} // 일단 클래스 정의 후 Car.prototype.refuel = function() { // 연료 공급 구현하는 코드 } Car.prototype.drive = function() { // 운전을 구현하는 코드 }
ES6
class Car { constructor(brand, name, color) {---생략---} refuel() { // 연료 공급 구현하는 코드 } drive() { // 연료 공급 구현하는 코드 } }
let avante = new Car('hyundai', 'avante', 'black'); avante.color; // 'black' avante.drive() // 아반떼가 운전을 시작합니다.
ES5
ES6
class Car { constructor(brand, name, color) { this.brand = brand; this.name = name; this.color = color; } drive() { console.log(this.name + '가 운전을 시작합니다.') } } let avante = new Car('hyundai', 'avante', 'black') avante.color; avante.drive();