>  기사  >  웹 프론트엔드  >  proto와 Constructor.prototype 구별: 핵심 차이점은 무엇입니까?

proto와 Constructor.prototype 구별: 핵심 차이점은 무엇입니까?

DDD
DDD원래의
2024-10-21 10:32:03856검색

Differentiating proto and Constructor.prototype: What's the Key Distinction?

proto와 Constructor.prototype

proto 속성과 constructor.prototype은 JavaScript에서 자주 혼동을 일으키는 밀접하게 관련된 개념입니다. 이 글은 이들의 차이점을 명확히 하는 것을 목표로 합니다.

__proto__:

proto는 프로토타입 객체를 가리키는 JavaScript 객체의 내부 속성입니다. 프로토타입 객체에는 해당 객체의 인스턴스가 상속한 속성과 메서드가 포함되어 있습니다. 개체는 생성자 함수에서 proto 속성을 ​​상속합니다.

예제에서 newtoy.__proto__는 다음을 포함하는 Gadget.prototype 개체를 반환합니다. 상속된 rating 속성.

constructor.prototype:

함수의 constructor.prototype 속성은 프로토타입을 참조합니다. 함수의 객체. new 키워드를 사용하여 객체를 생성하면 해당 생성자 함수의 프로토타입이 새 객체의 프로토타입이 됩니다.

이 예에서 newtoy.constructor.prototype은 반환됩니다. 상속된 등급 속성이 있는 Gadget.prototype 개체.

프로토타입 체인:

둘 다 proto 및 constructor.prototype은 객체가 프로토타입 객체로부터 속성과 메서드를 상속받을 수 있도록 하는 JavaScript의 메커니즘인 프로토타입 체인에 참여합니다.

newtoy.__proto__.constructor.prototype.constructor. 프로토타입.constructor.prototypeFunction.prototype에서 상속되고 궁극적으로 Object.prototype.에서 끝나는 Gadget.prototype

객체를 반환합니다.

Internet Explorer:

Internet Explorer에는 proto 속성이 없습니다. 이 컨텍스트에서 null을 확인하려면 hasOwnProperty() 메서드를 사용하여 객체에 특정 속성이 포함되어 있는지 확인할 수 있습니다.

예:

<code class="javascript">if (Object.hasOwnProperty("__proto__")) {
  // __proto__ property is available
} else {
  // __proto__ property is not available
}</code>

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

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