[JS] 프로토타입과 클래스
·
개념정리/JavaScript
💡 프로토타입 JavaScript에서 상속을 위해 사용하는 매커니즘 클래스 JavaScript에는 class가 없기 때문에 생성자 함수를 사용해서 그 역할을 대신합니다. class Human { constructor(name, age) { this.name = name; this.age = age; this.sayHello = function() { console.log(this.name + " said hello"); } } } new 키워드를 이용해 Human생성자 함수를 실행하고 전달 인자로 name과 age를 줍니다. let steve = new Human('steve', 30); steve.sayHello(); // "steve said hello" let kim = new Human("kim"..
[JS] 클래스와 인스턴스
·
개념정리/JavaScript
💡 객체 지향 프로그래밍 하나의 모델이 되는 청사진(blueprint)을 만들고, 그 청사진을 바탕으로 한 객체를 만드는 프로그래밍 패턴. 청사진은 클래스(class)라고 부르고, 청사진을 바탕으로 만든 객체를 인스턴스 객체(instance object) 줄여서 인스턴스 라고 부른다. 클래스를 만드는 방법 ES5이전 문법 function Car(brand, name, color) { this.brand = brand; this.name = name; this.color = color; } // 속성 Car.prototype.refuel = function(){ return "연료 공급"; } // 메서드 Car.prototype.drive = function(){ return "운전 시작"; } // 메..