Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is prototype in JavaScript

Understanding Prototypes in JavaScript

Prototypes in JavaScript can be a tricky concept to grasp, especially if you're just starting out on your programming journey. But don't worry, we're here to make it simple and understandable. Let's dive in!

What is a Prototype?

A prototype in JavaScript is a mechanism that allows objects to inherit features from other objects. In simpler terms, imagine a prototype as a blueprint that an object uses to learn about its properties and methods. It's like a kid learning how to do things from their parents - the parents here act as the prototypes.

A Basic Example of Prototype

Let's say we have an object named myCar and we want to find out the make of the car. If myCar doesn't have a property named make, JavaScript will look at myCar's prototype to see if that property exists there. If it doesn't find it there, it will go to the prototype's prototype, and so forth, until it gets to the end of the prototype chain.

let carPrototype = {
    make: 'Generic Car'

let myCar = Object.create(carPrototype);

console.log(myCar.make); //outputs 'Generic Car'

In this example, myCar does not have a make property of its own. But because myCar's prototype (carPrototype) does, myCar is able to access it.

Prototypes and Constructors

In JavaScript, every function has a prototype property, which is used when new objects are created from it using the new keyword. This property is initially an empty object, and all instances created from this constructor function will have access to this prototype object's properties and methods.

function Car(make, model, year) {
    this.make = make;
    this.model = model;
    this.year = year;

Car.prototype.getDetails = function() {
    return `${this.make} ${this.model} (${this.year})`;

let myCar = new Car('Toyota', 'Corolla', 2007);

console.log(myCar.getDetails()); //outputs 'Toyota Corolla (2007)'

In this example, we define a Car constructor function. We then assign a method getDetails to Car's prototype. Now, every instance of Car will have access to this method.

How the Prototype Chain Works

The prototype chain is a series of links between objects that JavaScript traverses until it finds the property it's looking for or reaches the end of the chain. It's like a family tree, with each object checking with its parent (prototype) if it doesn't know something.

let carPrototype = {
    make: 'Generic Car'

let myCar = Object.create(carPrototype);
myCar.model = 'Model X';

console.log(myCar.make); //outputs 'Generic Car'
console.log(myCar.model); //outputs 'Model X'

In this example, myCar doesn't have a make property, so it checks with carPrototype. It finds the property there, so it uses it. When checking for the model property, it finds it on itself, so it doesn't need to check with carPrototype.

Why Prototypes Matter?

Prototypes are fundamental to JavaScript because they provide a way to share behavior across objects, reducing memory consumption and increasing performance. They're like a library of code that your objects can access whenever they need to, rather than having to carry around their own copies of everything.

Wrapping Up

Prototypes are like the wise old wizards of JavaScript, passing on their knowledge (properties and methods) to the new objects created from them. They're crucial for sharing behavior and ensuring efficient memory utilization. Understanding them can take you from a JavaScript Padawan to a Jedi Master. So keep practicing, keep experimenting, and remember, as Yoda would say, "In JavaScript, there is no try... only do... or do not!"