const circle = {
radius: 5,
getDiameter() {
return 2 * Math.PI * this.radius;
}
}
상속: 어떤 객체의 포로퍼티 또는 메서드를 다른 객체가 상속받아 그대로 사용할 수 있는 것 -프로토타입을 기반으로 구현.
프로토타입(내 생각): 자바스크립트에서 상속을 구현하기 위해 생성자 함수의 프로토타입 프로퍼티에 바인딩 되어있는 객체??
생성자 함수가 생성한 모든 인스턴스는 상위 객체의 prototype의 모든 프로퍼티와 메서드를 상속받는다.
메서드 중복 생성 방지 - 메모리 낭비 방지 & 재사용성
function Circle(radius) {
this.radius = radius;
}
Circle.prototype.getArea = function () {
return Math.PI * this.radius ** 2;
}
const circle1 = new Circle(1);
const circle2 = new Circle(2);
console.log(circle1.getArea === circle2.getArea); //true