물론 자바스크립트에서도 클래스가 있습니다.
class Vehicle_ constructor(name,speed){ this.name = name; this.speed = speed; } speedUp(){ this.speed += 10; } speedDown(){ this.speed -= 10; } info(){ console.log(`${this.name}의 현재 속도는 ${this.speed}`); } } // 인스턴스 작성 const vehicle1 = new Vehicle('스포츠카',10); vehicle1.info(); vehicle1.speedUp(); vehicle1.speedUp(); vehicle1.speedUp(); vehicle1.speedDown(); vehicle1.info();
그리고 ES2022 부터 private가 지원됩니다.
class Person{ #idNo; constructor(name,age,idNo) { this.name = name; this.age = age; this.#idNo = idNo; } getIdNo(){ console.log(`idNo: ${this.#idNo}`); } } const person1 = new Person('rickter',100,'123'); person1.getIdNo();
클래스 상속입니다.
당연히 클래스는 상속이 되겠죠~class Vehicle{ constructor(speed){ this.speed = speed; } speedUp(){ this.speed +=10; } speedDown(){ this.speed -= 10; } info(){ console.log(`현재 속도는 ${this.speed}`); } } const vehicle1 = new Vehicle(100); vehicle1.info(); vehicle1.speedUp(); vehicle1.speedDown() vehicle1.info(); class Car extends Vehicle{ constructor(speed,wheels,seats) { super(speed); this.wheels = wheels; this.seats = seats; } drive(){console.log(`현재 속도는 ${this.speed}로 운행`)} } const car = new Car(100,4,4); car.speedUp(); console.log(car.speed); car.drive();