2013-04-25 3 views
1
constructor의 무한 체인 인 이유
function Test() 
{ 
    this.name = 'test name'; 
} 


console.log(Test.prototype.constructor.prototype.constructor); 

내가 이해할 수없는 - prototype?자바 스크립트 프로토 타입 생성자 혼란

이 체인의 목적은 무엇이며 왜 끝이 없는지, 프로토 타입에는 생성자가 있으며 생성자에는 프로토 타입, 루프 체인 및 생성자가 매번 같아 상상할 수 없다는 것을 의미합니다. ...

답변

3

음, 기본적으로 모든 기능 개체이 함수의 프로토 타입 객체를 참조 (생성자으로 사용하는 경우에만 중요하게)을 .prototype 속성이 있습니다.

모든 prototype object은 기본적으로 함수에 대한 참조를 가지고 있습니다. 물론이 함수는 생성자 함수 (귀하의 경우 Test())로 되돌아갑니다.

그래서, 우리가이 원하는 동작입니다

Test.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor 
+0

hmmm 또한 prototype.constructor.constructor.constructor. - 믿을 수 없지만, 여전히 이해하려고 노력하고 있습니다. 감사합니다. – Hello

+0

@Hello'.prototype.constructor.constructor'를주의 깊게 보면,이 함수는'Function()'기본 생성자를 참조합니다. 함수는이 언어의 * object *라는 것을 기억하십시오. – jAndy

0

이동합니다. constructor 프로토 타입의 속성은 프로토 타입을 소유 한 개체를 참조합니다 (따라서 자신을 다시 참조 함).

관련 문제