🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEjavascript

javascript Documentation

LOADING ENGINE...

Prototypes

AI & DATA SCIENCE // prototypes

Every JavaScript object has a prototype — another object it inherits properties and methods from. This forms the 'prototype chain'.

Syntax

const obj = {};
console.log(Object.getPrototypeOf(obj) === Object.prototype); // true

function Animal(name) { this.name = name; }
Animal.prototype.speak = function() { return this.name; };

Deep Dive Course

JavaScript uses **prototypal inheritance**. When you access a property, the engine first looks on the object itself, then walks up the **prototype chain** until found or null is reached. `Object.prototype` is at the top. Every function has a `.prototype` property used when constructors create instances.

1Understanding Prototypes

JavaScript uses prototypal inheritance. When you access a property, the engine first looks on the object itself, then walks up the prototype chain until found or null is reached. Object.prototype is at the top. Every function has a .prototype property used when constructors create instances.

💡

Object.create(null) creates an object with no prototype — useful for pure hash maps without inherited methods like toString().

editor.html
function Vehicle(type) { this.type = type; }
Vehicle.prototype.describe = function() {
  return `I am a ${this.type}`;
};

const car = new Vehicle('car');
console.log(car.describe());       // I am a car
console.log(car.hasOwnProperty('type'));     // true
console.log(car.hasOwnProperty('describe')); // false (on proto)
localhost:3000

2Practical Example

Here is a real-world application of Prototypes showing how it is used in production JavaScript code.

editor.html
// Prototype chain lookup
const animal = { breathes: true };
const dog = Object.create(animal);
dog.name = 'Rex';

console.log(dog.name);     // Rex (own)
console.log(dog.breathes); // true (from animal prototype)

const chain = [];
let proto = dog;
while (proto) { chain.push(proto); proto = Object.getPrototypeOf(proto); }
console.log(chain.length); // 3: dog, animal, Object.prototype
localhost:3000

3Best Practices

Follow these guidelines when working with Prototypes:

1. Use Object.getPrototypeOf() instead of __proto__

2. Add shared methods to Constructor.prototype (not instance)

3. Use hasOwnProperty() to check own vs inherited properties

⚠️

Tip: Object.create(null) creates an object with no prototype — useful for pure hash maps without inherited methods like toString().

editor.html
function Vehicle(type) { this.type = type; }
Vehicle.prototype.describe = function() {
  return `I am a ${this.type}`;
};

const car = new Vehicle('car');
console.log(car.describe());       // I am a car
console.log(car.hasOwnProperty('type'));     // true
console.log(car.hasOwnProperty('describe')); // false (on proto)
localhost:3000

Examples

Example 01Basic Usage
function Vehicle(type) { this.type = type; }
Vehicle.prototype.describe = function() {
  return `I am a ${this.type}`;
};

const car = new Vehicle('car');
console.log(car.describe());       // I am a car
console.log(car.hasOwnProperty('type'));     // true
console.log(car.hasOwnProperty('describe')); // false (on proto)
Example 02Advanced Example
// Prototype chain lookup
const animal = { breathes: true };
const dog = Object.create(animal);
dog.name = 'Rex';

console.log(dog.name);     // Rex (own)
console.log(dog.breathes); // true (from animal prototype)

const chain = [];
let proto = dog;
while (proto) { chain.push(proto); proto = Object.getPrototypeOf(proto); }
console.log(chain.length); // 3: dog, animal, Object.prototype

Best Practices

  • Use Object.getPrototypeOf() instead of __proto__
  • Add shared methods to Constructor.prototype (not instance)
  • Use hasOwnProperty() to check own vs inherited properties

Interview Question

What is the difference between __proto__ and prototype?

Hint: One is on instances, one is on functions.

__proto__ (now standardized as Object.getPrototypeOf()) is the actual prototype link of an INSTANCE. 'prototype' is a property of FUNCTIONS — it becomes the __proto__ of objects created with 'new'. Only functions have a 'prototype' property.

Exercises

MediumPractice using Prototypes in a real scenario.
View Solution
function Vehicle(type) { this.type = type; }
Vehicle.prototype.describe = function() {
  return `I am a ${this.type}`;
};

const car = new Vehicle('car');
console.log(car.describe());       // I am a car
console.log(car.hasOwnProperty('type'));     // true
console.log(car.hasOwnProperty('describe')); // false (on proto)

Frequently Asked Questions

What is the difference between __proto__ and prototype?

__proto__ (now standardized as Object.getPrototypeOf()) is the actual prototype link of an INSTANCE. 'prototype' is a property of FUNCTIONS — it becomes the __proto__ of objects created with 'new'. Only functions have a 'prototype' property.

Related Functions

InheritanceConstructorsClassesObject-Creating