mozilla

Revision 70201 of Differential inheritance in JavaScript

  • Revision slug: Differential_inheritance_in_JavaScript
  • Revision title: Differential inheritance in JavaScript
  • Revision id: 70201
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment one or more formatting changes
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(){
  // Extract the prototype from the "this" object
  var p = Object.getPrototypeOf(this);

  // Create a new object with p as its prototype
  return Object.create(p);
};

Object.getPrototypeOf is the standard ECMAScript 5 method. Alternatively, the non-standard __proto__ property can be used in web browsers supporting it.

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(){
  // Extract the prototype from the "this" object
  var p = Object.getPrototypeOf(this);

  // Create a new object with p as its prototype
  return Object.create(p);
};
</pre>
<p><code>Object.getPrototypeOf</code> is the standard ECMAScript 5 method. Alternatively, the non-standard <code>__proto__</code> property can be used in web browsers supporting it.</p>
<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/JavaScript/Reference/Global_Objects/Object/GetPrototypeOf" title="GetPrototypeOf">Object.getPrototypeOf</a></li> <li><a href="/en/JavaScript/Reference/Global_Objects/Object/proto" title="__proto__">__proto__</a></li>
</ul>
Revert to this revision