💡 Prototype와 __proto__ 가 헷갈린 건 이번이 몇번째 인지 모르겠어서 기록을 통해 정리를 하려고 한다... 정리해봤자 나중에 분명 또 햇갈리겠지만.. 이 Posting을 보면 금방 다시 상기 시킬 수 있을 듯 하다!
결론 부터 말하자면 Prototype과 __proto__가 지칭하는 객체는 동일하며 ( 글쓴이가 가정한 상황 속에서는 ) prototype은 생성자 함수의 property에서 그리고 __proto__ 는 생성자 함수를 통해 생성된 새로운 객체의 property에서 확인 할 수 있다.
function car(brand,price){
this.brand=brand
this.price=price
}
위와 같이 car라는 function을 정의하는 순간 자바스크립트 내에서는 car라는 객체가 생성되는 동시에 ( 자바스크립트 에서 함수도 객체 ) car.prototype이라는 객체도 생성됩니다. 그리고 그 prototype 객체에는 constructor라는 property가 있고 그 property값은 함수를 가리키는데 그 함수는 우리가 정의한 car를 지칭합니다. ( 하단 console 확인 )
let myFirstCar = new car('kia',16000);
상단 code에 더하여 위와같이 new연산자와 생성자 함수 ( 여기서는 car 함수 ) 를 통해서 새로운 객체를 만든 후 console을 찍어 본 결과 아래와 같이 __proto__ 가 있음을 확인 할 수 있습니다. 여기서 __proto__ 가 지칭하는 것은 생성자 함수 ( car ) 의 prototype과 동일한 객체입니다.
'Javascript' 카테고리의 다른 글
프로그래머스 코딩테스트 풀이(js) > 숫자 변환하기 (lv2) (0) | 2023.06.11 |
---|---|
프로그래머스 코딩테스트 풀이(js) > 최댓값과 최솟값 (lv2) (0) | 2023.06.04 |
[Javascript] es6 import, export 예시 (0) | 2022.07.10 |
[javascript] scroll 위치에 따른 navi bar 변경하기 (0) | 2022.06.22 |
[Javascript] 블록레벨 vs 함수레벨 scope (0) | 2022.06.09 |