나는 작은 코드를 가지고 : 코드가 표시Javascript가 constructor.prototype을 사용하여 수퍼 클래스 멤버를 방문하지 못합니까?
function father(){
this.id=10
}
function child(){}
console.log(child.prototype.constructor);//[Function: child]
child.prototype=new father();
//child.prototype.constructor = child; // key line
var son=new child();
console.log(child.prototype.constructor);//[Function: father]
console.log(son.constructor.prototype.id);//undefined.
, 나는 "아들"개체를 만들 수 프로토 타입 체인을 사용했다. 그러나 마지막 행은
"undefined".
이 나에게 이상한 인쇄합니다. child.prototype.constructor는 [Function : father]이고 "id"는 실제로 "father"의 속성이며 왜 정의되지 않은 것을 인쇄합니까? 내가
child.prototype.constructor = child; // key line
의 keyline 주석을 제거하면 내가 예상대로
는 그런 다음 "10"를 인쇄합니다. 키 라인을 가지고있는 것과 그렇지 않은 것의 차이점은 child.prototype.constructor는 'child'또는 'father'입니다. 그러나 'id'는 아버지 속성이므로 내 코드에 키 라인을 설정해야하는 이유는 무엇입니까?
감사합니다.
'id'는'father' 나'father.prototype'의 속성이 아닙니다. 그것은 '아버지'인스턴스의 속성입니다! – Bergi
나는 나쁜 이미지로 대답하려고 노력했다. :) – Oxi