JavaScript의 모든 객체에는 프로토타입 이라는 내장 속성이 있습니다. 프로토타입은 그 자체로 객체이므로 프로토타입은 자체 프로토타입을 갖게 되어 프로토타입 체인 이라고 불리는 것을 만듭니다 . null
자체 프로토타입 이 있는 프로토타입에 도달하면 체인이 종료됩니다 .
개체의 속성에 액세스하려고 할 때 개체 자체에서 속성을 찾을 수 없는 경우 프로토타입에서 속성을 검색합니다. 여전히 속성을 찾을 수 없으면 프로토타입의 프로토타입을 검색하고 속성을 찾거나 체인의 끝에 도달할 때까지 계속 검색합니다. 이 경우 undefined
가 반환됩니다.
Object.getPrototypeOf()
를 쓰면 됨)For better support, prefer Object.getPrototypeOf(), Reflect.getPrototypeOf() and Object.setPrototypeOf()/Reflect.setPrototypeOf() instead.
그 외 프로토타입 메서드 : constructor, hasOwnProperty, isPrototypeOf, valueOf, _defineGetter, …
외부에서 직접 접근은 불가능함.
1. __proto__
2. Object.getPrototypeOf()
3. Object.setPrototypeOf()
생성자 함수에서는 prototype 으로 접근 가능.
+)
new 생성자 함수 사용
예시
Dog.prototype = Object.create(Animal.prototype) function Dog(name, emoji, owner){ Animal.call(this, name,emoji){ this.owner = owner;
mixin(Object.assign(Dog.prototype, 프로토타입을 할당한 변수 1, 프로토타입 변수2)) : 여러개의 프로토타입을 상속할 때 쓰임.
최신 방법은 클래스(class, extends, super)!!
🔸Object.prototype.hasOwnProperty()
Return value : Returns true if the object has the specified property as own property; false otherwise.
The hasOwnProperty()
method returns true
if the specified property is a direct property of the object — even if the value is null
or undefined
. The method returns false
if the property is inherited, or has not been declared at all. Unlike the in
operator, this method does not check for the specified property in the object's prototype chain.
🔗 참고 사이트 : MDN