Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is a prototype in JavaScript

Understanding Prototypes in JavaScript

Before we delve into the heart of the matter, let's try to understand what a "prototype" is. In the simplest terms, a prototype is a blueprint for creating objects in JavaScript. It's like a master plan or a mold from which other objects are created.

To give you a better intuition, let's think of a prototype as a recipe. A recipe gives you all the ingredients and steps you need to make a dish. Similarly, a prototype provides the properties and methods that an object needs to operate.

JavaScript and Objects

JavaScript is an object-oriented programming language. An object, in this context, is a standalone entity with properties and types. Think of it as a 'thing' in the real world with a distinct identity. For instance, a car can be an object. It has properties like color, model, and methods like start, stop.

let car = {
    color: 'red',
    model: 'sedan',
    start: function() { console.log('Car started') },
    stop: function() { console.log('Car stopped') }
}

The Role of Prototypes

The main role of prototypes in JavaScript is to implement inheritance, a core principle in object-oriented programming. Inheritance allows objects to share properties and methods, just like children inherit features from their parents.

For example, if we have a general object called Animal, with properties like name and methods like eat, we can create another object, say Dog, that inherits these features from the Animal object. This avoids redundancy and makes our code cleaner.

let Animal = {
    name: '',
    eat: function() { console.log(this.name + ' is eating') }
}

let Dog = Object.create(Animal);
Dog.name = 'Rover';
Dog.eat(); // Output: Rover is eating

In this example, Animal is a prototype of Dog.

How to Access a Prototype

In JavaScript, every object has a property called prototype, and you can access it using Object.prototype. However, it's important to note that this prototype property is not the actual prototype (proto) of the object, but the object that will be assigned as the prototype to instances created with that function.

Here's how you can add a function to the Animal prototype that will then be inherited by all objects created from it.

Animal.prototype.sleep = function() {
    console.log(this.name + ' is sleeping');
}

Dog.sleep(); // Output: Rover is sleeping

Prototype Chain

When you try to access a property or a method of an object, JavaScript will first look at the object's own properties. If it doesn't find it there, it will look at the properties of the object's prototype, i.e., the properties and methods it inherited. This process continues up the prototype chain until JavaScript either finds the property/method or reaches the end of the chain. This is similar to how you would search for a book in a library. You would first check the shelf, then the entire section, and finally the whole library.

console.log(Dog.hasOwnProperty('name')); // Output: true
console.log(Dog.hasOwnProperty('sleep')); // Output: false

In this case, name is an own property of Dog, whereas sleep is a property of its prototype Animal.

The Power of Prototypes

The power of prototypes lies in their ability to enable dynamic inheritance. This means you can add new properties or methods to a prototype, and all objects inheriting from this prototype will immediately have access to these new features. It's like updating the recipe of a dish, and all future dishes made from that recipe will include the new ingredients.

Animal.prototype.run = function() {
    console.log(this.name + ' is running');
}

Dog.run(); // Output: Rover is running

Conclusion

Understanding prototypes in JavaScript is like learning the secrets of a magic trick. At first, it might seem complex and intimidating, but once you grasp the logic behind it, you'll not only appreciate the cleverness of the trick but also gain the power to perform it yourself.

Prototypes are a fundamental part of JavaScript, and mastering them will make you a better, more efficient coder. After all, why would you write the same code repeatedly when you can simply define it once and let all other objects inherit it? Remember, in the kingdom of code, DRY (Don't Repeat Yourself) is the law. So, keep exploring, keep learning, and let the magic of JavaScript prototypes empower you in your coding journey.