JavaScript Object Prototypes


Every JavaScript object gets its properties and methods from a prototype.


In the last section, we discovered the utilization of an object constructor:

We also found out that it's not possible to add a new characteristic to an already existing object constructor.

To include a fresh characteristic in a constructor, you need to incorporate it within the constructor function:


Prototype Inheritance

All JavaScript objects get their properties and methods from a prototype:

  • Date objects come from Date.prototype.
  • Array objects come from the Array.prototype.
  • Person things come from Person.prototype things.

The Object.prototype is at the beginning of the prototype inheritance chain.

Date, Array, andPerson objects all get their features from Object.prototype.


Adding Properties and Methods to Objects

At times, you might want to include additional characteristics (or actions) to all current objects of a specific kind.

At times, you may need to include additional features or actions to an object's constructor.


Using the prototype Property

The JavaScript prototype property lets you add additional properties to object constructors.

You can use the JavaScript prototype property to add new methods to object constructors.

Only change your own prototypes. Don't change the basic designs of regular JavaScript objects.