codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🌐

JavaScript

31 / 65 topics
28JavaScript Objects29JavaScript Object Methods & this30JavaScript Constructor Functions31JavaScript Prototypes32JavaScript Classes33JavaScript Class Inheritance34JavaScript Getters and Setters
Tutorials/JavaScript/JavaScript Prototypes
🌐JavaScript

JavaScript Prototypes

Updated 2026-05-12
30 min read

JavaScript Prototypes

In the previous tutorial, we explored JavaScript Constructor Functions and learned how they are used to create objects. While constructor functions provide a way to instantiate multiple objects with similar properties, they can become cumbersome when it comes to adding methods to each object individually. This is where prototypes come in.

Prototypes are a fundamental concept in JavaScript that allow for prototypal inheritance. They enable objects to inherit properties and methods from other objects, promoting code reusability and efficiency. Understanding prototypes is crucial for mastering object-oriented programming (OOP) in JavaScript.

In this tutorial, we will dive deep into JavaScript prototypes, explore how they work, and learn how to add properties and methods to them effectively. By the end of this tutorial, you'll have a solid understanding of how prototypes enhance your JavaScript applications.

Introduction

JavaScript is a prototype-based language, meaning that every object in JavaScript has an internal link to another object called its prototype. This prototype object can also have its own prototype, and so on, forming a chain known as the prototype chain. When you try to access a property or method on an object, JavaScript will first look for it on the object itself. If it doesn't find it, it will traverse up the prototype chain until it finds the property or reaches the end of the chain (i.e., null).

Prototypes are particularly useful in OOP because they allow you to define methods and properties that can be shared across multiple objects, reducing memory usage and improving performance.

Core Content

Understanding Prototypes

Let's start by understanding how prototypes work with a simple example. Consider the following constructor function:

script.js
1function Person(name) {
2this.name = name;
3}
4
5const alice = new Person('Alice');
6const bob = new Person('Bob');

In this example, alice and bob are instances of the Person constructor function. Each instance has its own name property.

Now, let's add a method to the Person prototype:

script.js
1function Person(name) {
2this.name = name;
3}
4
5Person.prototype.greet = function() {
6return `Hello, my name is ${this.name}!`;
7};
8
9const alice = new Person('Alice');
10const bob = new Person('Bob');
11
12console.log(alice.greet()); // Output: Hello, my name is Alice!
13console.log(bob.greet()); // Output: Hello, my name is Bob!
Output
Hello, my name is Alice!
Hello, my name is Bob!

In this example, the greet method is added to the Person.prototype. Both alice and bob can access this method because they inherit it from their prototype.

Adding Properties to Prototypes

You can also add properties to prototypes. However, be cautious when adding mutable properties to prototypes, as changes to these properties will affect all instances that share the same prototype.

Here's an example:

script.js
1function Person(name) {
2this.name = name;
3}
4
5Person.prototype.age = 30;
6
7const alice = new Person('Alice');
8const bob = new Person('Bob');
9
10console.log(alice.age); // Output: 30
11console.log(bob.age); // Output: 30
12
13alice.age = 25;
14console.log(alice.age); // Output: 25
15console.log(bob.age); // Output: 30
Output
30
30
25
30

In this example, the age property is added to the Person.prototype. Both alice and bob initially have an age of 30. However, when we change alice.age, it doesn't affect bob.age because alice now has its own age property.

Common Pitfalls

  1. Mutable Properties: As shown in the previous example, adding mutable properties to prototypes can lead to unexpected behavior if not handled carefully.
  2. Prototype Pollution: Adding properties or methods to built-in objects' prototypes (like Array.prototype) can cause conflicts and bugs in your code. It's generally a good practice to avoid modifying built-in prototypes unless absolutely necessary.

Practical Example

Let's create a practical example that demonstrates the use of prototypes in a real-world scenario. We'll build a simple application for managing a library with books and authors.

library.js
1function Book(title, author) {
2this.title = title;
3this.author = author;
4}
5
6Book.prototype.displayInfo = function() {
7return `${this.title} by ${this.author}`;
8};
9
10function Author(name) {
11this.name = name;
12this.books = [];
13}
14
15Author.prototype.addBook = function(book) {
16if (book instanceof Book) {
17 this.books.push(book);
18} else {
19 throw new Error('Invalid book');
20}
21};
22
23Author.prototype.listBooks = function() {
24return this.books.map(book => book.displayInfo()).join(', ');
25};
26
27const author1 = new Author('J.K. Rowling');
28const book1 = new Book('Harry Potter and the Sorcerer's Stone', 'J.K. Rowling');
29const book2 = new Book('Harry Potter and the Chamber of Secrets', 'J.K. Rowling');
30
31author1.addBook(book1);
32author1.addBook(book2);
33
34console.log(author1.listBooks());
35// Output: Harry Potter and the Sorcerer's Stone by J.K. Rowling, Harry Potter and the Chamber of Secrets by J.K. Rowling
Output
Harry Potter and the Sorcerer's Stone by J.K. Rowling, Harry Potter and the Chamber of Secrets by J.K. Rowling

In this example:

  • We define two constructor functions: Book and Author.
  • We add methods to their prototypes (displayInfo, addBook, and listBooks) to manage books and authors.
  • We create instances of Author and Book, and use the prototype methods to add books to an author and list them.

This example demonstrates how prototypes can be used to organize and share functionality across multiple objects, making your code more modular and maintainable.

Summary

In this tutorial, we explored JavaScript prototypes and their role in prototypal inheritance. We learned:

  • How prototypes work and how they form the prototype chain.
  • How to add properties and methods to prototypes to promote code reusability.
  • The importance of avoiding mutable properties and prototype pollution.

Understanding prototypes is essential for effective object-oriented programming in JavaScript, allowing you to create more efficient and organized applications.

What's Next?

In the next tutorial, we will dive into JavaScript Classes, which provide a more modern and syntactically cleaner way to implement OOP concepts. Classes are built on top of prototypes but offer a more familiar syntax for developers coming from other programming languages. By the end of this series, you'll have a comprehensive understanding of JavaScript's object-oriented features.

Stay tuned!


PreviousJavaScript Constructor FunctionsNext JavaScript Classes

Recommended Gear

JavaScript Constructor FunctionsJavaScript Classes