Course contentsShow
← JavaScript
Lesson 712 of 2,72317. Prototypes and InheritanceFree lesson

The 'prototype' Property on Functions

Every function has a 'prototype' property used when the function is called with 'new'.

The 'prototype' Property on Functions

What you'll learn: How every function has a built-in prototype property that becomes the blueprint for objects created with new.


The Special Role of Function Prototypes

Every function in JavaScript automatically gets a prototype property when it's created. This property is only meaningful when you use that function as a constructor with the new keyword. When you do, any object created by that constructor will have its internal prototype (what Object.getPrototypeOf() returns) linked to the function's prototype property.

Think of it like a cookie cutter and a recipe book: the function itself is the cookie cutter that shapes new objects, while the function's prototype property is the recipe book that says "all cookies made with this cutter should have these shared abilities."

How It Works

function Dog(name) {
  this.name = name;
}

// Every function has a 'prototype' property:
console.log(Dog.prototype); // {}

// Add methods to the function's prototype:
Dog.prototype.bark = function() {
  console.log(this.name + ' says woof!');
};

// When we use 'new', the created object inherits from Dog.prototype:
const buddy = new Dog('Buddy');
buddy.bark(); // "Buddy says woof!"

// The object's internal prototype IS the function's prototype property:
console.log(Object.getPrototypeOf(buddy) === Dog.prototype); // true

Notice we're adding methods to Dog.prototype, not directly to Dog. This makes those methods available to all instances created with new Dog() through the prototype chain.

Key Takeaway: Every function has a prototype property that serves as the template for objects created with new—the new object's internal prototype will point to that function's prototype property, establishing the prototype chain.