자바스크립트/Vanilla Js
Vanilla JS - classes (자바스크립트 객체 지향 프로그래밍)
RightHot
2019. 5. 21. 23:10
Vanilla JS - classes (자바스크립트 객체 지향 프로그래밍)
프로그래밍 패러다임
함수형 프로그래밍(Functional Programming)
객체 지향 프로그래밍(OOP-ObjectOrientedProgramming)
ES6 부터 자바스크립트에서 객체 지향 프로그래밍이 가능하다.
class Human{
constructor(name, lastName){
this.name = name;
this.lastName = lastName;
}
}
// Human 상속
const myName = new Human("Right","Hot");
console.log(myName);
console.log(myName.name);
console.log(myName.lastName);
class Baby extends Human{
cry(){
console.log("Waaaaaaaaa");
}
sayName(){
console.log(`My name is ${this.name} ${this.lastName}`)
}
}
const myBaby = new Baby("RightHot","Jr");
console.log(myBaby);
myBaby.cry();
myBaby.sayName();
Human {name: "Right", lastName: "Hot"}
Right
Hot
Baby {name: "RightHot", lastName: "Jr"}
Waaaaaaaaa
My name is RightHot Jr