💻 Programming

Prototype

Prototype

JavaScript's inheritance mechanism. An object inherits properties and methods from another object.

📖 Detailed Explanation

A prototype (Prototype) is JavaScript's object inheritance mechanism. Every JavaScript object has an internal slot called [[Prototype]], through which it inherits the properties and methods of other objects. Unlike class-based inheritance, it uses prototype-based inheritance, in which an object inherits directly from another object.

When you access an object's property and it does not exist, the search proceeds up the prototype chain. For example, calling arr.toString() searches in the order of the array object → Array.prototype → Object.prototype. This is called the prototype chain.

__proto__ used to be a non-standard way to access an object's prototype, but since ES6, Object.getPrototypeOf() and Object.setPrototypeOf() have become the standard. The prototype property of a constructor function becomes the [[Prototype]] of instances created with new.

The class syntax of ES6 is syntactic sugar for prototypes. Even when written with class, it works internally on a prototype basis. Understanding prototypes lets you deeply understand JavaScript's inheritance, this binding, extension of built-in objects, and more.

💻 Code Example

// JavaScript 프로토타입 예제

// 1. 프로토타입 체인 확인
const arr = [1, 2, 3];
console.log(arr.__proto__ === Array.prototype);  // true
console.log(Array.prototype.__proto__ === Object.prototype);  // true
console.log(Object.prototype.__proto__);  // null (체인의 끝)

// 2. 생성자 함수와 프로토타입
function Person(name) {
    this.name = name;
}

Person.prototype.greet = function() {
    console.log(`Hello, I'm ${this.name}`);
};

const alice = new Person('Alice');
alice.greet();  // "Hello, I'm Alice"
console.log(alice.__proto__ === Person.prototype);  // true

// 3. Object.create로 상속
const animal = {
    speak() {
        console.log(`${this.name} makes a sound`);
    }
};

const dog = Object.create(animal);
dog.name = 'Rex';
dog.bark = function() {
    console.log('Woof!');
};
dog.speak();  // "Rex makes a sound"

// 4. ES6 클래스 (프로토타입의 문법적 설탕)
class Vehicle {
    constructor(brand) {
        this.brand = brand;
    }

    info() {
        return `Brand: ${this.brand}`;
    }
}

class Car extends Vehicle {
    constructor(brand, model) {
        super(brand);
        this.model = model;
    }

    info() {
        return `${super.info()}, Model: ${this.model}`;
    }
}

// 5. 프로토타입 메서드 확인
console.log(Car.prototype.isPrototypeOf(new Car('Toyota', 'Camry')));  // true

🗣️ Examples of Practical Conversations

Senior: I want to add a custom method to arrays. Should I modify Array.prototype?

Junior: Then it would apply to all arrays!

Senior: Yes, but modifying built-in prototypes is risky. It can conflict with other libraries, so it's safer to make it a utility function or a subclass.

Interviewer: Please explain the difference between __proto__ and prototype.

Candidate: __proto__ is an internal link that every object has, and it points to that object's prototype. prototype is held only by function objects, and when an instance is created with new, the instance's __proto__ refers to this prototype.

Interviewer: What is the property lookup order in the prototype chain?

Candidate: It first looks in the object itself, and if not found, it searches upward along __proto__. If it is still not found even after reaching Object.prototype, it returns undefined.

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

Reviewer: You added a method to Object.prototype, and that's an anti-pattern.

Author: I wanted to use it conveniently on all objects.

Reviewer: Unexpected properties appear in for...in loops, and it can conflict with other libraries. Separate it into a utility function.

⚠️ Cautions

🔗 Related Terms

📚 Learn More