Prototype (JavaScript)

A prototype is the object that another object inherits properties and methods from. JavaScript uses prototype-based inheritance instead of classical classes under the hood.

JavaScript

Every JavaScript object has a hidden link to a prototype object. When you access a property that the object lacks, JavaScript looks up the prototype chain to find it.

The class syntax is a friendlier wrapper around this prototype mechanism.

Examples

JavaScript

function Dog(name) {
  this.name = name;
}
Dog.prototype.speak = function () {
  return this.name + " says woof";
};
console.log(new Dog("Rex").speak());

Output:

Rex says woof

See also

Back to the Dictionary