Prototype Chain

katsukichi·2021년 2월 26일
0

CodeStates_IM

목록 보기
13/48

legacy적인 문법 표현

프로토타입 체인이 뭐지?

__proto__이것으로 접근하는게 프로토타입 체인이다.

생성자.prototype===인스턴스.__proto__

Student.prototype = Object.create(Human.prototype);

이런방법으로 상속시킬수도 있다. 다만 좋은방법은아니다.!

proto체인을 했을때 명확하지않다.

상속을 정확하게 시키려면

Student.prototype = Object.create(Human.prototype);
Student.prototype.constructor = Student; // 이부분이 필요하다.
Student.prototype.learn = function(){}

하지만 이렇게하면 컨텍스트가 Human까지 닿지않는다. (undefined가된다)

var Student = function(name){
  Human.call(this, name); // Human.apply(this,arguments)
}

이렇게 넘겨줘야 컨텍스트가 넘어간다..

이렇게 어렵게 하는이유? 분명한 OOP를 설명하기위해서.

class를 사용하면

사실 클래스 쓰면 더쉽다는것.

class Student extends Human {
  constructor(name) {
    super(name);
  }
}

constructor 생김새가 자식과 부모가 같으면 , 자식꺼는 생략가능

다형성 ? 아마 오버라이딩 에 관련해서 나오지않을까

profile
front-back / end developer / Let's be an adaptable person

0개의 댓글