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 443077 of Differential inheritance in JavaScript

  • Revision slug: Differential_inheritance_in_JavaScript
  • Revision title: Differential inheritance in JavaScript
  • Revision id: 443077
  • Created:
  • Creator: Bergi
  • Is current revision? No
  • Comment Fixed the `clone` method to do what it was supposed to do (see also first version of this article)
Tags: 

Revision Content

Introduction

Differential Inheritance is a common prototype-oriented model that uses the concept that most objects are derived from other, more generic objects, and only differ in a few small aspects. Each object maintains a reference to its prototype and a table of properties that are different.

Example

The following code provides a simple method for "cloning" an object:

Object.prototype.clone = function(){
  // Create a new object with this as its prototype
  var p = Object.create(this);

  /* actually not necessary:
  // Apply the constructor on the new object
  this.constructor.apply(p, arguments);
  */

  return p;
};

Using clone, it becomes possible to simply derive more specific objects from a generic prototype. The following is a simple example of building up increasingly more specific objects using the clone method and differential inheritance.

var root = {}; // Could be any object with any prototype object

var record = root.clone();
record.toString = function(){ return "a Record"; };
 
var person = root.clone();
person.firstName = false;
person.lastName = false;
person.toString = function(){ 
    return this.firstName ?
               (this.lastName ?
                   this.firstName + " " +this.lastName :
                   this.firstName) :
               (this.lastName ?
                   this.lastName :
                   "a Person");
};
 
JoePerson = person.clone();
JoePerson.firstName = "Joe";
alert( JoePerson.toString() );

See also

Revision Source

<h2 id="Introduction">Introduction</h2>
<p>Differential Inheritance is a common prototype-oriented model that uses the concept that most objects are derived from other, more generic objects, and only differ in a few small aspects. Each object maintains a reference to its prototype and a table of properties that are different.</p>
<h2 id="Example">Example</h2>
<p>The following code provides a simple method for "cloning" an object:</p>
<pre class="brush: js">
Object.prototype.clone = function(){
  // Create a new object with this as its prototype
  var p = Object.create(this);

  /* actually not necessary:
  // Apply the constructor on the new object
  this.constructor.apply(p, arguments);
  */

  return p;
};
</pre>
<p>Using <code>clone</code>, it becomes possible to simply derive more specific objects from a generic prototype. The following is a simple example of building up increasingly more specific objects using the <code>clone</code> method and differential inheritance.</p>
<pre class="brush: js">
var root = {}; // Could be any object with any prototype object

var record = root.clone();
record.toString = function(){ return "a Record"; };
 
var person = root.clone();
person.firstName = false;
person.lastName = false;
person.toString = function(){ 
    return this.firstName ?
               (this.lastName ?
                   this.firstName + " " +this.lastName :
                   this.firstName) :
               (this.lastName ?
                   this.lastName :
                   "a Person");
};
 
JoePerson = person.clone();
JoePerson.firstName = "Joe";
alert( JoePerson.toString() );
</pre>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en/JavaScript/Reference/Global_Objects/Object/create" title="create">Object.create</a></li>
  <li><a href="/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain">inheritance and the prototype chain</a></li>
</ul>
Revert to this revision