class

릭터·2026년 4월 8일

Javascript

목록 보기
12/13

물론 자바스크립트에서도 클래스가 있습니다.

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();
profile
풀스택 개발자를 꿈 꾸는 릭터입니다.

0개의 댓글