>  기사  >  웹 프론트엔드  >  JavaScript에서 proto와 Constructor.prototype의 차이점은 무엇입니까?

JavaScript에서 proto와 Constructor.prototype의 차이점은 무엇입니까?

Patricia Arquette
Patricia Arquette원래의
2024-10-21 10:08:02843검색

What is the Difference Between proto and Constructor.prototype in JavaScript?

proto와 constructor.prototype

객체의 proto 속성의 차이점 공유 속성과 메서드가 포함된 프로토타입 개체를 참조합니다. 대조적으로 constructor.prototype은 객체 생성자 함수의 프로토타입 속성을 가리킵니다.

다음 예에서는 차이점을 보여줍니다.

<code class="javascript">function Gadget(name, color) {
  this.name = name;
  this.color = color;
}

Gadget.prototype.rating = 3;

var newtoy = new Gadget("webcam", "black");</code>

이 경우 newtoy.__proto__는 Gadget을 가리킵니다. 속성 등급이 있는 프로토타입과 newtoy.constructor.prototype도 Gadget.prototype을 가리킵니다. 그러나 newtoy.constructor.prototype.constructor.prototype.constructor.prototype은 Object.prototype 이외의 프로토타입이 없기 때문에 null을 반환합니다.

이는 proto가 Prototype에 대한 직접 참조이기 때문입니다. 프로토타입 객체이고 constructor.prototype은 프로토타입 체인을 따릅니다. constructor.prototype에 여러 번 액세스하면 최상위 Object.prototype에 도달할 때까지 프로토타입 체인을 순회하게 됩니다.

Internet Explorer에는 __proto__ 속성이 없습니다. 대신 [[Prototype]] 속성을 사용하여 객체의 프로토타입에 액세스할 수 있습니다. 그러나 표준 JavaScript 코드에서는 이 속성에 액세스할 수 없습니다.

프로토타입 개체를 참조하면 JavaScript의 상속 계층 구조를 이해하는 데 도움이 될 수 있으며 관련 개체 간에 속성과 메서드를 공유하는 메커니즘을 제공할 수 있습니다.

위 내용은 JavaScript에서 proto와 Constructor.prototype의 차이점은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.