Dev.to WebDev 🛠 Dev 👁 0 📖 3 min read

Understanding Prototypes in JavaScript: A Practical Guide for Developers

JavaScript uses prototypes to support inheritance, property lookup, and shared behavior between objects. Although modern syntax gives us classes and familiar object-oriented patterns, prototypes remain the mechanism unde

JavaScript uses prototypes to support inheritance, property lookup, and shared behavior between objects. Although modern syntax gives us classes and familiar object-oriented patterns, prototypes remain the mechanism underneath many of those features.

Understanding prototypes makes JavaScript behavior much easier to reason about. It helps explain why methods can be shared across instances, how property lookup works, and why objects created with constructors can access methods they do not directly own.

How JavaScript Prototypes and Prototype Chains Work

Every JavaScript object can have an internal prototype reference. When you access a property that does not exist directly on an object, JavaScript searches its prototype, then that prototype's prototype, continuing until it finds the property or reaches null.

A constructor function can define shared behavior through its prototype object. This is useful because methods placed on the prototype are created once and shared by all instances instead of creating a separate function for every object.

Modern JavaScript classes provide cleaner syntax for working with this model, but class methods are also placed on the prototype. The prototype chain is therefore an important foundation for understanding both traditional constructor functions and modern class-based JavaScript.

The following example demonstrates direct properties, prototype methods, property lookup, constructor functions, instanceof, and the prototype chain step by step.

function User(name, role) {
  // Instance properties belong directly to each created object.
  this.name = name;
  this.role = role;
}

// Add a shared method to the constructor's prototype.
// Every User instance can access this method.
User.prototype.describe = function () {
  return `${this.name} works as a ${this.role}.`;
};

// Add another shared method to demonstrate prototype behavior.
User.prototype.isDeveloper = function () {
  return this.role.toLowerCase().includes("developer");
};

console.log("1. Creating User instances...");

const ansh = new User("Ansh", "JavaScript Developer");
const riya = new User("Riya", "Product Designer");

console.log("Ansh:", ansh);
console.log("Riya:", riya);

console.log("\\n2. Checking own properties...");

console.log("Ansh has own 'name':", ansh.hasOwnProperty("name"));
console.log("Ansh has own 'describe':", ansh.hasOwnProperty("describe"));

// The name property exists directly on the instance.
// The describe method exists on User.prototype.
console.log("Ansh name:", ansh.name);
console.log("Ansh describe():", ansh.describe());

console.log("\\n3. Checking the prototype relationship...");

console.log(
  "Ansh prototype is User.prototype:",
  Object.getPrototypeOf(ansh) === User.prototype
);

console.log(
  "Riya prototype is User.prototype:",
  Object.getPrototypeOf(riya) === User.prototype
);

console.log("\\n4. Demonstrating shared prototype methods...");

console.log("Ansh is developer:", ansh.isDeveloper());
console.log("Riya is developer:", riya.isDeveloper());

// Both instances reference the same function stored on the prototype.
console.log(
  "Same describe function:",
  ansh.describe === riya.describe
);

console.log("\\n5. Understanding property lookup...");

// JavaScript first checks the object itself.
// If the property is missing, it checks User.prototype.
console.log("describe found through prototype:", "describe" in ansh);
console.log("isDeveloper found through prototype:", "isDeveloper" in ansh);

console.log("\\n6. Inspecting the prototype chain...");

const userPrototype = Object.getPrototypeOf(ansh);
const objectPrototype = Object.getPrototypeOf(userPrototype);

console.log("First prototype:", userPrototype === User.prototype);
console.log("Second prototype is Object.prototype:", objectPrototype === Object.prototype);
console.log("End of chain:", Object.getPrototypeOf(objectPrototype) === null);

console.log("\\n7. Using instanceof...");

console.log("ansh instanceof User:", ansh instanceof User);
console.log("riya instanceof User:", riya instanceof User);
console.log("ansh instanceof Object:", ansh instanceof Object);

console.log("\\n8. Updating shared behavior...");

// Changing the prototype method affects existing and future instances.
User.prototype.greet = function () {
  return `Hello, ${this.name}!`;
};

console.log("Ansh greeting:", ansh.greet());
console.log("Riya greeting:", riya.greet());

console.log("\\n9. Demonstrating property shadowing...");

// Create an instance property with the same name as a prototype property.
ansh.role = "Senior JavaScript Developer";
console.log("Updated Ansh role:", ansh.role);
console.log("Prototype role exists:", User.prototype.role);

console.log("\\n10. Final prototype summary...");
console.log("Own keys:", Object.keys(ansh));
console.log("Prototype keys:", Object.keys(User.prototype));
console.log("Constructor name:", User.prototype.constructor.name);

Conclusion

Prototypes are one of the core mechanisms behind JavaScript's object system. They allow objects to share behavior while keeping instance-specific data separate, which can reduce unnecessary function duplication and provide a flexible inheritance model.

The most important concept is the prototype chain: JavaScript checks an object's own properties first and then walks upward through its prototypes when necessary. Once this lookup process becomes familiar, features such as classes, instanceof, constructor functions, and shared methods become much easier to understand.

For practical JavaScript development, you do not need to manipulate prototypes constantly. However, knowing how they work helps when debugging unexpected property behavior, working with libraries, optimizing object design, or understanding what JavaScript classes are doing underneath the syntax.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.