학습 log (이론)/javascript

'프로토타입 체이닝' 상세

abbear25 2016. 10. 19. 14:23

기본 원칙

자바스크립트에서는 모든 객체는 자신을 생성한 생성자 함수의 

prototype 프로퍼티가 가리키는 객체를 자신의 부모 객체로 취급함 


객체 리터럴 방식으로 생성된 객체의 프로토타입 체이닝

*literal, 직접 값을 나타내는 자구단위

myObject 객체 -> Object.prototype 객체


var myObject = {

name: 'illua',

call: function(){

console.log('My name is '+name);

}

}; 

myObject.call();    //결과: My name is illua

console.log(myObject.hasOwnProperty('name'));    //결과: true

console.log(myObject.hasOwnProperty('nickName'));    //결과: false

myObject.calling(); //결과: Uncaught TypeError


*hasOwnProperty()

이 메서드를 호출한 객체에 인자로 넘긴 문자열 이름의 프로퍼티나

메서드가 있는지 체크해주는 표준 API

*myObject에는 hasOwnProperty가 없지만 [[prototype]]링크를 따라서

Object.prototype객체에 접근하여 사용


생성자 함수로 생성된 객체의 프로토타입 체이닝

person 객체 -> myObject 객체 -> Object.prototype 객체 


function myObject(name){

this.name = name;

}

var person = new myObject('illua');

console.log(person.hasOwnProperty('name'));    //결과: true


Object.prototype

프로토타입 체이닝의 종점

반응형