프로토타입(Prototype)과 클래스(Class)

따봉도치 개발자·2023년 3월 15일
0

프로토타입(prototype)

프로토타입(Prototype) 기반 언어 입니다. 여기서 프로토타입은 원형 객체를 의미한다. 또한, 프로토타입 객체도 또 다시 상위 프로토타입 객체로부터 메소드와 속성을 상속 받을 수도 있고 그 상위 프로토타입 객체도 마찬가지로 받을 수 있다. 이를 프로토타입 체인(prototype chain)이라고 부른다.

function Person(first, last, age, gender, interests) {
	this.name = {
  		this.first = first;
 		this.last = last;
}
	this.age = age;
    this.gender = gender;
    this.interests = interests;
    Person.prototype.bio = function() {
    var string = this.name.first + ' ' + this.name.last + ' is ' + this.age + ' years old. ';
        var pronoun;
}
var person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);

person1.valueOf()
여기서 메소드 속성들이 다른 객체로 복사되는 것이 아니라 체인을 타고 올라가며 접근할 뿐이다.
브라우저는 우선 person1 객체가 valueOf() 메소드를 가지고 있는지 체크합니다. 없다면 person1의 프로토타입 객체(Person() 생성자의 프로토타입)에 valueOf() 메소드가 있는지 체크합니다. 여전히 없으므로 Person() 생성자의 프로토타입 객체의 프로토타입 객체(Object() 생성자의 프로토타입)가 valueOf() 메소드를 가지고 있는지 체크합니다. 여기에 있으니 호출하며 끝납니다!


__proto__ - 프로퍼티나 메서드에 접근하기 위해 사용하는 속성이다.

profile
Explain Like I'm 5

0개의 댓글