Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1118197 of Object prototypes

  • Revision slug: Learn/JavaScript/Objects/Object_prototypes
  • Revision title: Object prototypes
  • Revision id: 1118197
  • Created:
  • Creator: ZeroUnderscoreOu
  • Is current revision? No
  • Comment Fixed a 404 on "Previous" button

Revision Content

{{draft}}{{LearnSidebar}}
{{PreviousMenuNext("Learn/JavaScript/Objects/Object-oriented_JS", "Learn/JavaScript/Objects/Inheritance", "Learn/JavaScript/Objects")}}

Prototypes are the mechanism by which JavaScript objects inherit features from one another, and they work differently to inheritance mechanisms in classical object-oriented programming languages. In this article we explore that difference, explain how prototype chains work, and look at how the protoype property can be used to add methods to existing constructors.

Prerequisites: Basic computer literacy, a basic understanding of HTML and CSS, familiarity with JavaScript basics (see First steps and Building blocks) and OOJS basics (see Introduction to objects).
Objective: To understand JavaScript object prototypes, how prototype chains work, and how to add new methods onto the prototype property.

A prototype-based language??

JavaScript is often described as a prototype-based language — each object has a prototype object, which acts as a template object that it inherits methods and properties from. An object's prototype object may also have a prototype object, which it inherits methods and properties from, and so on. This is often referred to as a prototype chain, and explains why different objects have properties and methods defined on other objects available to them.

Well, to be exact, the properties and methods are defined on the Objects' constructor functions, not the object instances themselves. Let's look at an example to make this a bit clearer.

Understanding prototype objects

Let's go back to the example in which we finished writing our Person() constructor — load the example in your browser. If you don't still have it from above, use our oojs-class-further-exercises.html example (see also the source code).

In this example we have defined a constructor function, like so:

function Person(first, last, age, gender, interests) {
  
  // property and method definitions
  
};

We have then created an object instance like this:

var person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);

If you type person1. into your JavaScript console, you should see the browser try to autocomplete this with the member names available on this object:

In this list you will see the members defined on person1's prototype object, which is the Person() constructor — name, age, gender, interests, bio, and greeting. You will however also see some other members — watch, valueOf, etc — these are defined on the Person() constructor's prototype object, which is Object().  This demonstrates the prototype chain working.

[TEMPORARY DIAGRAM— WILL REPLACE WITH SOMETHING BETTER SOON]

person1 -----------------> Person ---------------------> Object
              inherits                   inherits
           from prototype              from prototype

So what happens if you call a method on person1, which is actually defined on Object? For example:

person1.valueOf()

This method simply returns the value of the object it is called on — try it and see! In this case, what happens is:

  • The browser initially checks to see if the person1 object has a valueOf() method available on it.
  • It doesn't, so the browser then checks to see if the person1 object's prototype object (Person) has a valueOf() method available on it.
  • It doesn't either, so the browser then checks to see if the Person() constructor's prototype object (Object) has a valueOf() method available on it. It does, so it is called, and all is good!

Note: The methods and properties are not copied from one object to another in the prototype chain — they are accessed by walking up the chain as described above.

Note: There isn't officially a way to access an object's prototype object directly — the "links" between the items in the chain are defined in an internal property, referred to as [[prototype]] in the specification for the JavaScript language (see {{glossary("ECMAScript")}}). Most modern browsers however do have a property available on them called __proto__ (that's 2 underscores either side), which contains the object's prototype object. For example, try person1.__proto__ and person1.__proto__.__proto__ to see what the chain looks like in code!

The prototype property: Where inherited members are defined

So, where are the inherited properties and methods defined? If you look at the Object() reference page, you'll see listed in the left hand side a large number of properties and methods — many more than the number of inherited members we saw available on the person1 object in the above screenshot. So some are inheritable, and some aren't — why is this?

The answer is that the inherited ones are the ones defined on the prototype property (you could call it a sub namespace) — that is, the ones that begin with Object.prototype., and not the ones that begin with just Object. The prototype property is basically a bucket for storing properties and methods that we want to be inherited by objects further down the prototype chain.

So Object.prototype.watch(), Object.prototype.valueOf(), etc., are available to any object types that inherit from Object(), including new object instances created from the constructor.

Object.is(), Object.keys(), and other members not defined inside the prototype bucket are not inherited by object instances or object types that inherit from Object(). They are methods/properties available just on the Object() constructor itself.

Note: This seems strange — how can you have a method defined on a constructor, which is itself a function? Well, a function is also a type of object — see the Function() constructor reference if you don't believe us.

  1. You can check out existing prototype properties for yourself — go back to our previous example and try entering the following into the JavaScript console:
    Person.prototype
  2. The output won't show you very much — after all, we haven't defined anything on our custom constuctor's prototype! By default, a constructor's prototype always starts empty. Now try the following:
    Object.prototype

You'll see a large number of methods defined on Object's prototype property, which are then available on objects that inherit from Object, as shown earlier.

You'll see other examples of prototype chain inheritance all over JavaScript — try looking for the methods and properties defined on the prototype of the String(), Date(), Number(), and Array() global objects, for example. These all have a number of members defined on their prototype, which is why for example when you create a string, like this:

var myString = 'This is my string.';

myString immediately has a number of useful methods available on it, like split(), indexOf(), replace(), etc.

Important: The prototype property is one of the most confusingly-named parts of JavaScript — you might think that this points to the prototype object of the current object, but it doesn't (that's an internal object that can be accessed by __proto__, remember?) prototype instead is a property containing an object on which you define members that you want to be inherited.

Revisiting create()

Earlier on we showed how the Object.create() method can be used to create a new object instance.

  1. For example, try this in your previous example's JavaScript console:
    var person2 = Object.create(person1);
  2. What create() actually does is to create a new object from a specified prototype object. Here, person2 is being created using person1 as a prototype object. You can check this by entering the following in the console:
    person2.__proto__

This will return the person1 object.

The constructor property

Every object instance has a constructor property, which points to the original constructor function that created the instance.

  1. For example, try these commands in the console:
    person1.constructor
    person2.constructor

    These should both return the Person() constructor, as it contains the original definition of these instances.

    A clever trick is that you can put parentheses onto the end of the constructor property (containing any required parameters) to create another object instance from that constructor. The constructor is a function after all, so can be invoked using parentheses; you just need to include the new keyword to specify that you want to use the function as a constructor.

  2. Try this in the console:
    var person3 = new person1.constructor('Karen', 'Stephenson', 26, 'female', ['playing drums', 'mountain climbing']);
  3. Now try accessing your new object's features, for example:
    person3.name.first
    person3.age
    person3.bio()

This works well. You won't need to use it often, but it can be really useful when you want to create a new instance and don't have a reference to the original constructor easily available for some reason.

Modifying prototypes

Let's have a look at an example of modifying the prototype property of a constructor.

  1. Go back to our oojs-class-further-exercises.html example and make a local copy of the source code. Below the existing JavaScript, add the following code, which adds a new method to the constructor's prototype property:
    Person.prototype.farewell = function() {
      alert(this.name.first + ' has left the building. Bye for now!');
    }
  2. Save the code and load the page in the browser, and try entering the following into the text input:
    person1.farewell();

You should get an alert message displayed, featuring the person's name as defined inside the constructor. This is really useful, but what is even more useful is that the whole inheritance chain has updated dynamically, automatically making this new method available on all object instances derived from the constructor.

Think about this for a moment. In our code we define the constructor, then we create an instance object from the constructor, then we add a new method to the contructor's prototype:

function Person(first, last, age, gender, interests) {

  // property and method definitions

};

var person1 = new Person('Tammi', 'Smith', 32, 'neutral', ['music', 'skiing', 'kickboxing']);

Person.prototype.farewell = function() {
  alert(this.name.first + ' has left the building. Bye for now!');
}

But the farewell() method is still available on the instance object. This proves what we said earlier about the prototype chain, and the browser looking upwards in the chain to find methods that aren't defined on the object instance itself rather than those methods being copied to the instance. This provides a very powerful, extensive system of functionality.

Note: If you are having trouble getting this example to work, have a look at our oojs-class-prototype.html example (see it running live also).

You will rarely see properties defined on the prototype property, because they are not very flexible when defined like this. For example you could add a property like so:

Person.prototype.fullName = 'Bob Smith';

But this isn't very flexible, as the person might not be called that. It'd be much better to do this, to build the fullName out of name.first and name.last:

Person.prototype.fullName = this.name.first + ' ' + this.name.last;

This however doesn't work, as this will be referencing the global in this case, not the function scope. Calling this property would return undefined undefined. This worked fine on the method we defined earlier in the prototype because it is sitting inside a function scope, which will be transferred successfully to the object instance scope. So you might define constant properties on the prototype (i.e. ones that never need to change), but generally it works better to define properties inside the constructor.

In fact, a fairly common pattern for more complex object definitions is to define the properties inside the constructor, and the methods on the prototype. This makes the code easier to read, as the constructor only contains the property definitions, and the methods are split off into separate blocks. For example:

// Constructor with property definitions

function Test(a,b,c,d) {
  // property definitions
};

// First method definition

Test.prototype.x = function () { ... }

// Second method definition

Test.prototype.y = function () { ... }

// etc.

This pattern can be seen in action in Piotr Zalewa's school plan app example.

Summary

This article has covered JavaScript object prototypes, including how prototype object chains allow objects to inherit features from one another, the prototype property and how it can be used to add methods to constructors, and other related topics.

In the next article we'll look at how you can implement inheritance of functionality between two of your own custom objects.

{{PreviousMenuNext("Learn/JavaScript/Objects/Object-oriented_JS", "Learn/JavaScript/Objects/Inheritance", "Learn/JavaScript/Objects")}}

Revision Source

<div>{{draft}}{{LearnSidebar}}</div>

<div>{{PreviousMenuNext("Learn/JavaScript/Objects/Object-oriented_JS", "Learn/JavaScript/Objects/Inheritance", "Learn/JavaScript/Objects")}}</div>

<p class="summary">Prototypes are the mechanism by which JavaScript objects inherit features from one another, and they work differently to inheritance mechanisms in classical object-oriented programming languages. In this article we explore that difference, explain how prototype chains work, and look at how the protoype property can be used to add methods to existing constructors.</p>

<table class="learn-box standard-table">
 <tbody>
  <tr>
   <th scope="row">Prerequisites:</th>
   <td>Basic computer literacy, a basic understanding of HTML and CSS, familiarity with JavaScript basics (see <a href="/en-US/docs/Learn/JavaScript/First_steps">First steps</a> and <a href="/en-US/docs/Learn/JavaScript/Building_blocks">Building blocks</a>) and OOJS basics (see <a href="/en-US/docs/Learn/JavaScript/Object-oriented/Introduction">Introduction to objects</a>).</td>
  </tr>
  <tr>
   <th scope="row">Objective:</th>
   <td>To understand JavaScript object prototypes, how prototype chains work, and how to add new methods onto the prototype property.</td>
  </tr>
 </tbody>
</table>

<h2 id="A_prototype-based_language">A prototype-based language??</h2>

<p>JavaScript is often described as a <strong>prototype-based language</strong> — each object has a <strong>prototype object</strong>, which acts as a template object that it inherits methods and properties from. An object's prototype object may also have a prototype object, which it inherits methods and properties from, and so on. This is often referred to as a <strong>prototype chain</strong>, and explains why different objects have properties and methods defined on other objects available to them.</p>

<p>Well, to be exact, the properties and methods are defined on the Objects' constructor functions, not the object instances themselves. Let's look at an example to make this a bit clearer.</p>

<h2 id="Understanding_prototype_objects">Understanding prototype objects</h2>

<p>Let's go back to the example in which we finished writing our <code>Person()</code> constructor — load the example in your browser. If you don't still have it from above, use our <a href="https://mdn.github.io/learning-area/javascript/oojs/introduction/oojs-class-further-exercises.html">oojs-class-further-exercises.html</a> example (see also the <a href="https://github.com/mdn/learning-area/blob/master/javascript/oojs/introduction/oojs-class-further-exercises.html">source code</a>).</p>

<p>In this example we have defined a constructor function, like so:</p>

<pre class="brush: js">
function Person(first, last, age, gender, interests) {
  
  // property and method definitions
  
};</pre>

<p>We have then created an object instance like this:</p>

<pre class="brush: js">
var person1 = new Person('Bob', 'Smith', 32, 'male', ['music', 'skiing']);</pre>

<p>If you type <code>person1.</code> into your JavaScript console, you should see the browser try to autocomplete this with the member names available on this object:</p>

<p><img alt="" src="https://mdn.mozillademos.org/files/13853/object-available-members.png" style="display:block; margin:0 auto" /></p>

<p>In this list you will see the members defined on <code>person1</code>'s prototype object, which is the <code>Person()</code> constructor — <code>name</code>, <code>age</code>, <code>gender</code>, <code>interests</code>, <code>bio</code>, and <code>greeting</code>. You will however also see some other members — <code>watch</code>, <code>valueOf</code>, etc — these are defined on the <code>Person()</code> constructor's prototype object, which is <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object()</a></code>.&nbsp; This demonstrates the prototype chain working.</p>

<p>[TEMPORARY DIAGRAM— WILL REPLACE WITH SOMETHING BETTER SOON]</p>

<pre>
person1 -----------------&gt; Person ---------------------&gt; Object
              inherits                   inherits
           from prototype              from prototype</pre>

<p>So what happens if you call a method on <code>person1</code>, which is actually defined on <code>Object</code>? For example:</p>

<pre class="brush: js">
person1.valueOf()</pre>

<p>This method simply returns the value of the object it is called on — try it and see! In this case, what happens is:</p>

<ul>
 <li>The browser initially checks to see if the <code>person1</code> object has a <code>valueOf()</code> method available on it.</li>
 <li>It doesn't, so the browser then checks to see if the <code>person1</code> object's prototype object (<code>Person</code>) has a <code>valueOf()</code> method available on it.</li>
 <li>It doesn't either, so the browser then checks to see if the <code>Person()</code> constructor's prototype object (<code>Object</code>) has a <code>valueOf()</code> method available on it. It does, so it is called, and all is good!</li>
</ul>

<div class="note">
<p><strong>Note</strong>: The methods and properties are <strong>not</strong> copied from one object to another in the prototype chain — they are accessed by walking up the chain as described above.</p>
</div>

<div class="note">
<p><strong>Note</strong>: There isn't officially a way to access an object's prototype object directly — the "links" between the items in the chain are defined in an internal property, referred to as <code>[[prototype]]</code> in the specification for the JavaScript language (see {{glossary("ECMAScript")}}). Most modern browsers however do have a property available on them called <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto">__proto__</a></code> (that's 2 underscores either side), which contains the object's prototype object. For example, try <code>person1.__proto__</code> and <code>person1.__proto__.__proto__</code> to see what the chain looks like in code!</p>
</div>

<h2 id="The_prototype_property_Where_inherited_members_are_defined">The prototype property: Where inherited members are defined</h2>

<p>So, where are the inherited properties and methods defined? If you look at the <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">Object()</a></code> reference page, you'll see listed in the left hand side a large number of properties and methods — many more than the number of inherited members we saw available on the <code>person1</code> object in the above screenshot. So some are inheritable, and some aren't — why is this?</p>

<p>The answer is that the inherited ones are the ones defined on the <code>prototype</code> property (you could call it a sub namespace) — that is, the ones that begin with <code>Object.prototype.</code>, and not the ones that begin with just <code>Object.</code> The <code>prototype</code> property is basically a bucket for storing properties and methods that we want to be inherited by objects further down the prototype chain.</p>

<p>So <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/watch">Object.prototype.watch()</a></code>, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf">Object.prototype.valueOf()</a></code>, etc., are available to any object types that inherit from <code>Object()</code>, including new object instances created from the constructor.</p>

<p><code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is">Object.is()</a></code>, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys">Object.keys()</a></code>, and other members not defined inside the <code>prototype</code> bucket are not inherited by object instances or object types that inherit from <code>Object()</code>. They are methods/properties available just on the <code>Object()</code> constructor itself.</p>

<div class="note">
<p><strong>Note</strong>: This seems strange — how can you have a method defined on a constructor, which is itself a function? Well, a function is also a type of object — see the <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function">Function()</a></code> constructor reference if you don't believe us.</p>
</div>

<ol>
 <li>You can check out existing prototype properties for yourself — go back to our previous example and try entering the following into the JavaScript console:
  <pre class="brush: js">
Person.prototype</pre>
 </li>
 <li>The output won't show you very much — after all, we haven't defined anything on our custom constuctor's prototype! By default, a constructor's <code>prototype</code> always starts empty. Now try the following:
  <pre class="brush: js">
Object.prototype</pre>
 </li>
</ol>

<p>You'll see a large number of methods defined on <code>Object</code>'s <code>prototype</code> property, which are then available on objects that inherit from <code>Object</code>, as shown earlier.</p>

<p>You'll see other examples of prototype chain inheritance all over JavaScript — try looking for the methods and properties defined on the prototype of the <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String">String()</a></code>, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date">Date()</a></code>, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number">Number()</a></code>, and <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array">Array()</a></code> global objects, for example. These all have a number of members defined on their prototype, which is why for example when you create a string, like this:</p>

<pre class="brush: js">
var myString = 'This is my string.';</pre>

<p><code>myString</code> immediately has a number of useful methods available on it, like <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split">split()</a></code>, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf">indexOf()</a></code>, <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace">replace()</a></code>, etc.</p>

<div class="warning">
<p><strong>Important</strong>: The <code>prototype</code> property is one of the most confusingly-named parts of JavaScript — you might think that this points to the prototype object of the current object, but it doesn't (that's an internal object that can be accessed by <code>__proto__</code>, remember?) prototype instead is a property containing an object on which you define members that you want to be inherited.</p>
</div>

<h2 id="Revisiting_create()">Revisiting create()</h2>

<p>Earlier on we showed how the <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create">Object.create()</a></code> method can be used to create a new object instance.</p>

<ol>
 <li>For example, try this in your previous example's JavaScript console:
  <pre class="brush: js">
var person2 = Object.create(person1);</pre>
 </li>
 <li>What <code>create()</code> actually does is to create a new object from a specified prototype object. Here, <code>person2</code> is being created using <code>person1</code> as a prototype object. You can check this by entering the following in the console:
  <pre class="brush: js">
person2.__proto__</pre>
 </li>
</ol>

<p>This will return the <code>person1</code> object.</p>

<h2 id="The_constructor_property">The constructor property</h2>

<p>Every object instance has a <code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor">constructor</a></code> property, which points to the original constructor function that created the instance.</p>

<ol>
 <li>For example, try these commands in the console:
  <pre>
person1.constructor
person2.constructor</pre>

  <p>These should both return the <code>Person()</code> constructor, as it contains the original definition of these instances.</p>

  <p>A clever trick is that you can put parentheses onto the end of the <code>constructor</code> property (containing any required parameters) to create another object instance from that constructor. The constructor is a function after all, so can be invoked using parentheses; you just need to include the <code>new</code> keyword to specify that you want to use the function as a constructor.</p>
 </li>
 <li>Try this in the console:
  <pre class="brush: js">
var person3 = new person1.constructor('Karen', 'Stephenson', 26, 'female', ['playing drums', 'mountain climbing']);</pre>
 </li>
 <li>Now try accessing your new object's features, for example:
  <pre class="brush: js">
person3.name.first
person3.age
person3.bio()</pre>
 </li>
</ol>

<p>This works well. You won't need to use it often, but it can be really useful when you want to create a new instance and don't have a reference to the original constructor easily available for some reason.</p>

<h2 id="Modifying_prototypes">Modifying prototypes</h2>

<p>Let's have a look at an example of modifying the <code>prototype</code> property of a constructor.</p>

<ol>
 <li>Go back to our <a href="https://mdn.github.io/learning-area/javascript/oojs/introduction/oojs-class-further-exercises.html">oojs-class-further-exercises.html</a> example and make a local copy of the <a href="https://github.com/mdn/learning-area/blob/master/javascript/oojs/introduction/oojs-class-further-exercises.html">source code</a>. Below the existing JavaScript, add the following code, which adds a new method to the constructor's <code>prototype</code> property:

  <pre class="brush: js">
Person.prototype.farewell = function() {
  alert(this.name.first + ' has left the building. Bye for now!');
}</pre>
 </li>
 <li>Save the code and load the page in the browser, and try entering the following into the text input:
  <pre class="brush: js">
person1.farewell();</pre>
 </li>
</ol>

<p>You should get an alert message displayed, featuring the person's name as defined inside the constructor. This is really useful, but what is even more useful is that the whole inheritance chain has updated dynamically, automatically making this new method available on all object instances derived from the constructor.</p>

<p>Think about this for a moment. In our code we define the constructor, then we create an instance object from the constructor, <em>then</em> we add a new method to the contructor's prototype:</p>

<pre class="brush: js">
function Person(first, last, age, gender, interests) {

  // property and method definitions

};

var person1 = new Person('Tammi', 'Smith', 32, 'neutral', ['music', 'skiing', 'kickboxing']);

Person.prototype.farewell = function() {
  alert(this.name.first + ' has left the building. Bye for now!');
}</pre>

<p>But the <code>farewell()</code> method is <em>still</em> available on the instance object. This proves what we said earlier about the prototype chain, and the browser looking upwards in the chain to find methods that aren't defined on the object instance itself rather than those methods being copied to the instance. This provides a very powerful, extensive system of functionality.</p>

<div class="note">
<p><strong>Note</strong>: If you are having trouble getting this example to work, have a look at our <a href="https://github.com/mdn/learning-area/blob/master/javascript/oojs/advanced/oojs-class-prototype.html">oojs-class-prototype.html</a> example (see it <a href="https://mdn.github.io/learning-area/javascript/oojs/advanced/oojs-class-prototype.html">running live</a> also).</p>
</div>

<p>You will rarely see properties defined on the <code>prototype</code> property, because they are not very flexible when defined like this. For example you could add a property like so:</p>

<pre>
Person.prototype.fullName = 'Bob Smith';</pre>

<p>But this isn't very flexible, as the person might not be called that. It'd be much better to do this, to build the <code>fullName</code> out of <code>name.first</code> and <code>name.last</code>:</p>

<pre>
Person.prototype.fullName = this.name.first + ' ' + this.name.last;</pre>

<p>This however doesn't work, as <code>this</code> will be referencing the global in this case, not the function scope. Calling this property would return <code>undefined undefined</code>. This worked fine on the method we defined earlier in the prototype because it is sitting inside a function scope, which will be transferred successfully to the object instance scope. So you might define constant properties on the prototype (i.e. ones that never need to change), but generally it works better to define properties inside the constructor.</p>

<p>In fact, a fairly common pattern for more complex object definitions is to define the properties inside the constructor, and the methods on the prototype. This makes the code easier to read, as the constructor only contains the property definitions, and the methods are split off into separate blocks. For example:</p>

<pre>
// Constructor with property definitions

function Test(a,b,c,d) {
  // property definitions
};

// First method definition

Test.prototype.x = function () { ... }

// Second method definition

Test.prototype.y = function () { ... }

// etc.</pre>

<p>This pattern can be seen in action in Piotr Zalewa's <a href="https://github.com/zalun/school-plan-app/blob/master/stage9/js/index.js">school plan app</a> example.</p>

<h2 id="Summary">Summary</h2>

<p>This article has covered JavaScript object prototypes, including how prototype object chains allow objects to inherit features from one another, the prototype property and how it can be used to add methods to constructors, and other related topics.</p>

<p>In the next article we'll look at how you can implement inheritance of functionality between two of your own custom objects.</p>

<p>{{PreviousMenuNext("Learn/JavaScript/Objects/Object-oriented_JS", "Learn/JavaScript/Objects/Inheritance", "Learn/JavaScript/Objects")}}</p>
Revert to this revision