<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

<title>Object Hierarchy and Inheritance in JavaScript
</title>
</head><body bgcolor="#ffffff" text="#000000">

<center><h2>Object Hierarchy and Inheritance in JavaScript</h2>
</center>
JavaScript is an object-oriented language based on prototypes, rather
than, as is common, being class-based. Because of this different basis,
it can be less apparent how JavaScript allows you to create hierarchies
of objects and to have inheritance of properties and their values. This
paper attempts to clarify the situation. If you're interested in
precise details of how this all works, you can read the ECMA-262
JavaScript language specification (as a </a><a href="http://developer.netscape.com/library/javascript/e262-pdf.pdf" target="_top">PDF file</a> or a <a href="http://developer.netscape.com/library/javascript/e262-doc.exe" target="_top">Microsoft Word self-extracting binary</a>).
<a name="1041991">
The sections in this paper are:</a></p><p>
</p><ul><a name="1041992">
</a><li><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1030757">The Employee Example</a></li>
<a name="1041997">
</a><li><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1030750">Creating the Hierarchy</a></li>
<a name="1042002">
</a><li><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1035076">Object Properties</a></li>
<a name="1042007">
</a><li><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1044609">More Flexible Constructors</a></li>
<a name="1042012">
</a><li><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1040208">Property Inheritance Revisited</a></li>
</ul><a name="1030639">
Class-based object-oriented languages, such as Java and C++, are
founded on the concept of two distinct entities: classes and instances.
A <b>class</b>
defines all of the properties (considering methods and fields in Java,
or members in C++, to be properties) that characterize a certain set of
objects. A class is an abstract thing, rather than any particular
member of the set of objects it describes. For example, the <code>Employee</code> class could represent the set of all employees. An <b>instance</b>, on the other hand, is the instantiation of a class; that is, one of its members. For example, <code>Victoria</code> could be an instance of the <code>Employee</code>
class, representing a particular individual as an employee. An instance
has exactly the properties of its parent class (no more, no less).</a><p>
<a name="1044714">
A prototype-based language, such as JavaScript, does not make this
distinction: it simply has objects. A prototype-based language has the
notion of a <b>prototypical object</b>,
an object used as a template from which to get the initial properties
for a new object. Any object can specify its own properties, either
when you create it or even at runtime. In addition, any object can be
associated as the <b>prototype</b> for another object, allowing the second object to share the first object's properties.</a></p><p>
<a name="1031000">
In class-based languages, you define a class in a separate <b>class definition</b>. In that definition you can specify special methods, called <b>constructors</b>,
to use to create instances of the class. A constructor method can
specify initial values for the instance's properties and perform other
processing appropriate at creation time. You use the <b>new</b> operator in association with the constructor method to create class instances.</a></p><p>
<a name="1031044">
JavaScript follows a similar model, but does not have a class
definition separate from the constructor. Instead, you define a
constructor function to create objects with a particular initial set of
properties and values. Any JavaScript function can be used as a
constructor. You use the <b>new</b> operator with a constructor function to create a new object. </a></p><p>
<a name="1030424">
In a class-based language, you create a hierarchy of classes through
the class definitions. In a class definition, you can specify that the
new class is a <b>subclass</b>
of an already existing class. The subclass inherits all the properties
of the superclass and additionally can add new properties or modify the
inherited ones. For example, assume the <code>Employee</code> class includes only <code>name</code> and <code>dept</code> properties and <code>Manager</code> is a subclass of <code>Employee</code> that adds the <code>reports</code> property. In this case, an instance of the <code>Manager</code> class would have all three properties: <code>name</code>, <code>dept</code>, and <code>reports</code>.</a></p><p>
<a name="1031408">
JavaScript implements inheritance by allowing you to associate a
prototypical object with any constructor function. So, you can create
exactly the <code>Employee</code>-<code>Manager</code> example, but you use slightly different terminology. First you define the <code>Employee</code> constructor function, specifying the <code>name</code> and <code>dept</code> properties. Next, you define the <code>Manager</code> constructor function, specifying the <code>reports</code> property. Finally, you assign a new <code>Employee</code> object as the <code>prototype</code> for the <code>Manager</code> constructor function. Then, when you create a new <code>Manager</code>, it inherits the <code>name</code> and <code>dept</code> properties from the <code>Employee</code> object.</a></p><p>
<a name="1030703">
In class-based languages, you typically create a class at compile time
and then you instantiate instances of the class either at compile time
or at runtime. You cannot change the number or the type of properties
of a class after you define the class. In JavaScript, however, at
runtime you can add or remove properties from any object. If you add a
property to an object that is used as the prototype for a set of
objects, the objects for which it is the prototype also get the new
property.</a></p><p>
<a name="1031978">
</a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1031117">Table&nbsp;1</a>
gives a short summary of some of these differences. The rest of this
paper describes the details of using JavaScript constructors and
prototypes to create an object hierarchy and compares this to how you
would do it in Java. </p><p><b><a name="1031117">
Table 1&nbsp;Comparison of class-based (Java) and prototype-based (JavaScript) object systems</a></b>
<table border="2">
<tbody><tr><th align="left" valign="baseline"><b><a name="1031121">
<b>Class-based (Java)
</b></a></b></th><th align="left" valign="baseline"><b><a name="1031123">
<b>Prototype-based (JavaScript)
</b></a></b>
</th></tr><tr><td align="left" valign="baseline"><a name="1031125">
Class and instance are distinct entities.</a></td><td align="left" valign="baseline"><a name="1031127">
All objects are instances.</a>
</td></tr><tr><td align="left" valign="baseline"><a name="1031142">
Define a class with a class definition; instantiate a class with constructor methods.</a></td><td align="left" valign="baseline"><a name="1031144">
Define and create a set of objects with constructor functions.</a>
</td></tr><tr><td align="left" valign="baseline"><a name="1031159">
Create a single object with the <code>new</code> operator.</a></td><td align="left" valign="baseline"><a name="1031161">
Same.</a>
</td></tr><tr><td align="left" valign="baseline"><a name="1031168">
Construct an object hierarchy by using class definitions to define subclasses of existing classes.</a></td><td align="left" valign="baseline"><a name="1031170">
Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.</a>
</td></tr><tr><td align="left" valign="baseline"><a name="1031248">
Inherit properties by following the class chain.</a></td><td align="left" valign="baseline"><a name="1031250">
Inherit properties by following the prototype chain.</a>
</td></tr><tr><td align="left" valign="baseline"><a name="1031331">
Class definition specifies <i>all</i> properties of all instances of a class. No way to add properties dynamically at runtime.</a></td><td align="left" valign="baseline"><a name="1031333">
Constructor function or prototype specifies an <i>initial set</i> of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects.</a>

</td></tr></tbody></table>
<table>
<tbody><tr><td>
</td></tr></tbody></table>
 </p><p>

<a name="The Employee Example"></a>
<a name="1030757">
<h2> The Employee Example</h2>
</a>
<a name="1032252">
The rest of this paper works with the simple employee hierarchy shown in </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1032301">Figure 1</a>. </p><p>
<a name="1032301">
<b>Figure 1&nbsp;&nbsp;&nbsp;  A simple object hierarchy </b></a></p><p>
<a name="1032301"><b><img src="jsoo_files/hier01.gif">

</b></a></p><p>


</p><p>
<a name="1032257">
</a></p><li><code><a name="1032257">Employee</a></code><a name="1032257"> has the properties <code>name</code> (whose value defaults to the empty string) and <code>dept</code> (whose value defaults to <code>"general"</code>).</a></li>
<p>
<a name="1032258">
</a></p><li><code><a name="1032258">Manager</a></code><a name="1032258"> is based on <code>Employee</code>. It adds the <code>reports</code> property (whose value defaults to an empty array, intended to have an array of <code>Employee</code> objects as its value).</a></li>
<p>
<a name="1032180">
</a></p><li><code><a name="1032180">WorkerBee</a></code><a name="1032180"> is also based on <code>Employee</code>. It adds the <code>projects</code> property (whose value defaults to an empty array, intended to have an array of strings as its value).</a></li>
<p>
<a name="1032195">
</a></p><li><code><a name="1032195">SalesPerson</a></code><a name="1032195"> is based on <code>WorkerBee</code>. It adds the <code>quota</code> property (whose value defaults to 100). It also overrides the <code>dept</code> property with the value <code>"sales"</code>, indicating that all salespersons are in the same department.</a></li>
<p>
<a name="1032216">
</a></p><li><code><a name="1032216">Engineer</a></code><a name="1032216"> is based on <code>WorkerBee</code>. It adds the <code>machine</code> property (whose value defaults to the empty string) and also overrides the <code>dept</code> property with the value <code>"engineering"</code>.</a></li>

<a name="Creating the Hierarchy"></a>
<a name="1030750">
<h2> Creating the Hierarchy</h2>
</a>
<a name="1034964">
There are several ways you can define appropriate constructor functions
to implement the Employee hierarchy. How you choose to define them
depends largely on what you want to be able to do in your application.
We'll get into all that later. </a><p>
<a name="1035051">
For now, let's use very simple (and comparatively inflexible)
definitions just to see how we get the inheritance to work. In these
definitions, you can't specify any property values when you create an
object. The newly-created object simply gets the default values, which
you can change at a later time. </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1035015">Figure 2</a> illustrates the hierarchy with these simple definitions.</p><p>
<a name="1046083">
In a real application, you would probably define constructors that
allow you to provide property values at object creation time. Options
for doing so are described later in </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1044609">"More Flexible Constructors"</a>. For now, these simple definitions let us look at how the inheritance occurs.</p><p>
<a name="1035015">
<b>Figure 2&nbsp;&nbsp;&nbsp;  What the definitions look like </b></a></p><p>
<a name="1035015"><b><img src="jsoo_files/hier02.gif">

</b></a></p><p>


<a name="1042271">
The simple Java and JavaScript <code>Employee</code>
definitions below are similar. The only difference is that you need to
specify the type for each property in Java but not in JavaScript and
you need to create an explicit constructor method for the Java class. </a></p><p><a name="1042271"><b></b>
</a><table border="2">
<tbody><tr><th align="left" valign="baseline"><b><a name="1044938">
<b>JavaScript
</b></a></b></th><th align="left" valign="baseline"><b><a name="1042338">
<b>Java
</b></a></b>
</th></tr><tr><td align="left" valign="baseline"><a name="1042340">
<pre>function Employee () {<br>    this.name = "";<br>    this.dept = "general";<br>}</pre></a></td><td align="left" valign="baseline"><a name="1042342">
<pre>public class Employee {<br>&nbsp;&nbsp;&nbsp;public String name;<br>&nbsp;&nbsp;&nbsp;public String dept;<br>&nbsp;&nbsp;&nbsp;public Employee () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name = "";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.dept = "general";<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a>

</td></tr></tbody></table>
<table>
<tbody><tr><td>
</td></tr></tbody></table>
</p><p>
<a name="1042297">
The <code>Manager</code> and WorkerBee definitions
show the difference in how you specify the next object higher in the
inheritance chain. In JavaScript, you add a prototypical instance as
the value of the <code>prototype</code> property of the constructor
function. You can do so at any time after you define the constructor.
In Java, you specify the superclass within the class definition. You
cannot change the superclass outside the class definition.</a></p><p><a name="1042297"><b></b>
</a><table border="2">
<tbody><tr><th align="left" valign="baseline"><b><a name="1042370">
<b>JavaScript
</b></a></b></th><th align="left" valign="baseline"><b><a name="1042372">
<b>Java
</b></a></b>
</th></tr><tr><td align="left" valign="baseline"><a name="1042378">
<pre>function Manager () {<br>    this.reports = [];<br>}<br>Manager.prototype = new Employee;</pre></a><a name="1042382">
<pre>function WorkerBee () {<br>    this.projects = [];<br>}<br>WorkerBee.prototype = new Employee;</pre></a></td><td align="left" valign="baseline"><a name="1042380">
<pre>public class Manager extends Employee {<br>&nbsp;&nbsp;&nbsp;public Employee[] reports;<br>&nbsp;&nbsp;&nbsp;public Manager () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.reports = new Employee[0];<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a><a name="1042384">
<pre>public class WorkerBee extends Employee {<br>&nbsp;&nbsp;&nbsp;public String[] projects;<br>&nbsp;&nbsp;&nbsp;public WorkerBee () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.projects = new String[0];<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a>

</td></tr></tbody></table>
<table>
<tbody><tr><td>
</td></tr></tbody></table>
</p><p>
<a name="1042306">
The <code>Engineer</code> and <code>SalesPerson</code> definitions create objects that descend from <code>WorkerBee</code> and hence from <code>Employee</code>.
An object of these types has properties of all the objects above it in
the chain. In addition, these definitions override the inherited value
of the <code>dept</code> property with new values specific to these objects. </a></p><p><a name="1042306"><b></b>
</a><table border="2">
<tbody><tr><th align="left" valign="baseline"><b><a name="1042437">
<b>JavaScript
</b></a></b></th><th align="left" valign="baseline"><b><a name="1042439">
<b>Java
</b></a></b>
</th></tr><tr><td align="left" valign="baseline"><a name="1042766">
<pre>function SalesPerson () {<br>&nbsp;&nbsp;&nbsp;this.dept = "sales";<br>&nbsp;&nbsp;&nbsp;this.quota = 100;<br>}<br>SalesPerson.prototype = new WorkerBee;</pre></a><a name="1042453">
<pre>function Engineer () {<br>&nbsp;&nbsp;&nbsp;this.dept = "engineering";<br>&nbsp;&nbsp;&nbsp;this.machine = "";<br>}<br>Engineer.prototype = new WorkerBee;</pre></a></td><td align="left" valign="baseline"><a name="1042768">
<pre>public class SalesPerson extends WorkerBee {<br>&nbsp;&nbsp;&nbsp;public double quota;<br>&nbsp;&nbsp;&nbsp;public SalesPerson () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.dept = "sales";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.quota = 100.0;<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a><a name="1042455">
<pre>public class Engineer extends WorkerBee {<br>&nbsp;&nbsp;&nbsp;public String machine;<br>&nbsp;&nbsp;&nbsp;public Engineer () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.dept = "engineering";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.machine = "";<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a>

</td></tr></tbody></table>
<table>
<tbody><tr><td>
</td></tr></tbody></table>
</p><p>
<a name="1033212">
Using these definitions, you can create instances of these objects that get the default values for their properties. </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1035230">Figure 3</a> illustrates using these JavaScript definitions to create new objects and shows the property values for the new objects.</p><p>
</p><blockquote><b>NOTE: </b><a name="1036464">
As described earlier, the term <b>instance</b> has a specific technical meaning in 
class-based languages. In these languages, an instance is an individual member 
of a class and is fundamentally different from a class. In JavaScript, "instance" 
does not have this technical meaning because JavaScript does not have this 
difference between classes and instances. However, in talking about JavaScript, 
"instance" can be used informally to mean an object created using a particular 
constructor function. So, in this example, you could informally say that <code>jane</code> is 
an instance of <code>Engineer</code>. Similarly, although the terms <b>parent</b>, <b>child</b>, 
<b>ancestor</b>, and <b>descendant</b> do not have formal meanings in JavaScript, we can 
use them informally to refer to objects higher or lower in the prototype chain.
</a></blockquote>

<a name="1035230">
<b>Figure 3&nbsp;&nbsp;&nbsp;  Creating objects with the simple definitions </b></a><p>
<a name="1035230"><b><img src="jsoo_files/hier03.gif">

</b></a></p><p>



<a name="Object Properties"></a>
<a name="1035076">
<h2> Object Properties</h2>
</a>
<a name="1044591">
This section discusses how objects inherit properties from other
objects in the prototype chain and what happens when you add a property
at runtime.</a></p><p>

<a name="Head2;"></a>
<a name="1042848">
<h3> Inheriting Properties</h3>
</a>

<a name="1037638">
Assume you create the <code>mark</code> object as a <code>WorkerBee</code> as shown in </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1035230">Figure 3</a> with this statement:</p><p>
<a name="1037887">
<pre>mark = new WorkerBee;</pre></a><a name="1037915">
When JavaScript sees the <code>new</code> operator, it creates a new generic object and passes this new object as the value of the <code>this</code> keyword to the <code>WorkerBee</code> constructor function. The constructor function explicitly sets the value of the <code>projects</code> property. It also sets the value of the internal <code>__proto__</code> property to the value of <code>WorkerBee.prototype</code>. (That property name has 2 underscore characters at the front and 2 at the end.) The <code>__proto__</code>
property determines the prototype chain used to return property values.
Once these properties are set, JavaScript returns the new object and
the assignment statement sets the variable <code>mark</code> to that object.</a></p><p>
<a name="1038044">
This process doesn't explicitly put values in the <code>mark</code> object (<b>local</b> values) for the properties <code>mark</code>
inherits from the prototype chain. When you ask for the value of a
property, JavaScript first checks to see if the value exists in that
object. If it does, that value is returned. If the value isn't there
locally, JavaScript checks the prototype chain (using the <code>__proto__</code>
property). If an object in the prototype chain has a value for the
property, that value is returned. If no such property is found,
JavaScript says the object doesn't have the property. In this way, the <code>mark</code> object has the following properties and values:</a></p><p>
<a name="1037606">
<pre>mark.name = "";<br>mark.dept = "general";<br>mark.projects = [];</pre></a><a name="1046148">
The <code>mark</code> object inherits values for the <code>name</code> and <code>dept</code> properties from the prototypical object in <code>mark.__proto__</code>. It is assigned a local value for the <code>projects</code> property by the <code>WorkerBee</code>
constructor. Simply put, this gives you inheritance of properties and
their values in JavaScript. Some subtleties of this process are
discussed in </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1040208">"Property Inheritance Revisited"</a>.</p><p>
<a name="1037968">
Because these constructors don't let you supply instance-specific
values, this information is generic. The property values are the
default ones shared by all new objects created from <code>WorkerBee</code>. You can, of course, change the values of any of these properties. So, you could give specific information for <code>mark</code> as shown here:</a></p><p>
<a name="1037728">
<pre>mark.name = "Doe, Mark";<br>mark.dept = "admin";<br>mark.projects = ["navigator"];</pre></a>
<a name="Head2;"></a>
<a name="1042890">
<h3> Adding Properties</h3>
</a>

<a name="1042885">
In JavaScript at runtime you can add properties to any object. You are
not constrained to use only the properties provided by the constructor
function. To add a property that is specific to a single object, you
simply assign a value to the object, as in:</a></p><p>
<a name="1037779">
<pre>mark.bonus = 3000;</pre></a><a name="1037547">
Now, the <code>mark</code> object has a <code>bonus</code> property, but no other <code>WorkerBee</code> has this property. </a></p><p>
<a name="1037800">
If you add a new property to an object that is being used as the
prototype for a constructor function, you add that property to all
objects that inherit properties from the prototype. For example, you
can add a <code>specialty</code> property to all employees with the following statement:</a></p><p>
<a name="1037805">
<pre>Employee.prototype.specialty = "none";</pre></a><a name="1037810">
As soon as JavaScript executes this statement, the <code>mark</code> object also has the <code>specialty</code> property with the value of <code>"none"</code>. </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1037175">Figure 4</a> shows the effect of adding this property to the <code>Employee</code> prototype and then overriding it for the <code>Engineer</code> prototype.</p><p>
<a name="1037175">
<b>Figure 4&nbsp;&nbsp;&nbsp;  Adding properties </b></a></p><p>
<a name="1037175"><b><img src="jsoo_files/hier04.gif">

</b></a></p><p>



<a name="More Flexible Constructors"></a>
<a name="1044609">
<h2> More Flexible Constructors</h2>
</a>
<a name="1041901">
The constructor functions used so far do not let you specify property
values when you create an instance. As with Java, you can provide
arguments to constructors to initialize property values for instances. </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1041961">Figure 5</a> shows one way to do this.</p><p>
<a name="1041961">
<b>Figure 5&nbsp;&nbsp;&nbsp;  Specifying properties in a constructor, take 1 </b></a></p><p>
<a name="1041961"><b><img src="jsoo_files/hier05.gif">

</b></a></p><p>


<a name="1046365">
Here are the Java and JavaScript definitions for these objects. </a></p><p><a name="1046365"><b></b>
</a><table border="2">
<tbody><tr><th align="left" valign="baseline"><b><a name="1041592">
<b>JavaScript
</b></a></b></th><th align="left" valign="baseline"><b><a name="1041594">
<b>Java 
</b></a></b>
</th></tr><tr><td align="left" valign="baseline"><a name="1041596">
<pre>function Employee (name, dept) {<br>    this.name = name || "";<br>    this.dept = dept || "general";<br>}</pre></a></td><td align="left" valign="baseline"><a name="1043223">
<pre>public class Employee {<br>&nbsp;&nbsp;&nbsp;public String name;<br>&nbsp;&nbsp;&nbsp;public String dept;<br>&nbsp;&nbsp;&nbsp;public Employee () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this("", "general");<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;public Employee (name) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this(name, "general");<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;public Employee (name, dept) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.name = name;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.dept = dept;<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a>
</td></tr><tr><td align="left" valign="baseline"><a name="1043103">
<pre>function WorkerBee (projs) {<br>    this.projects = projs || [];<br>}<br>WorkerBee.prototype = new Employee;</pre></a></td><td align="left" valign="baseline"><a name="1045331">
<pre>public class WorkerBee extends Employee {<br>&nbsp;&nbsp;&nbsp;public String[] projects;<br>&nbsp;&nbsp;&nbsp;public WorkerBee () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this(new String[0]);<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;public WorkerBee (String[] projs) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.projects = projs;<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a>
</td></tr><tr><td align="left" valign="baseline"><a name="1043124">
<pre>function Engineer (mach) {<br>&nbsp;&nbsp;&nbsp;this.dept = "engineering";<br>&nbsp;&nbsp;&nbsp;this.machine = mach || "";<br>}<br>Engineer.prototype = new WorkerBee;</pre></a></td><td align="left" valign="baseline"><a name="1043338">
<pre>public class Engineer extends WorkerBee {<br>&nbsp;&nbsp;&nbsp;public String machine;<br>&nbsp;&nbsp;&nbsp;public WorkerBee () {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.dept = "engineering";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.machine = "";<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;public WorkerBee (mach) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.dept = "engineering";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.machine = mach;<br>&nbsp;&nbsp;&nbsp;}<br>}</pre></a>

</td></tr></tbody></table>
<table>
<tbody><tr><td>
</td></tr></tbody></table>
</p><p>
<a name="1041608">
These JavaScript definitions use a special idiom for setting default values:</a></p><p>
<a name="1046531">
<pre>this.name = name || "";</pre></a><a name="1046524">
The JavaScript logical OR operator (<code>||</code>)
evaluates its first argument. If that argument is converts to true, the
operator returns it. Otherwise, the operator returns the value of the
second argument. Therefore, this line of code tests to see if <code>name</code> has a useful value for the <code>name</code> property. If it does, it sets <code>this.name</code> to that value. Otherwise, it sets <code>this.name</code> to the empty string. This paper uses this idiom for brevity; however, it can be puzzling at first glance. </a></p><p>
<a name="1046540">
With these definitions, when you create an instance of an object, you
can specify values for the locally defined properties. As shown in </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1041961">Figure 5</a>, you can use this statement to create a new <code>Engineer</code>:</p><p>
<a name="1039055">
<pre>jane = new Engineer("belau");</pre></a><a name="1039064">
Jane's properties are now:</a></p><p>
<a name="1039078">
<pre>jane.name == "";<br>jane.dept == "general";<br>jane.projects == [];<br>jane.machine == "belau"</pre></a><a name="1039079">
Notice that with these definitions, you cannot specify an initial value for an inherited property such as <code>name</code>.
If you want to specify an initial value for inherited properties in
JavaScript, you need to add more code to the constructor function.</a></p><p>
<a name="1039083">
So far, the constructor function has created a generic object and then
specified local properties and values for the new object. You can have
the constructor add more properties by directly calling the constructor
function for an object higher in the prototype chain. </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1039186">Figure 6</a> shows these new definitions. </p><p>
<a name="1039186">
<b>Figure 6&nbsp;&nbsp;&nbsp;  Specifying properties in a constructor, take 2 </b></a></p><p>
<a name="1039186"><b><img src="jsoo_files/hier06.gif">

</b></a></p><p>


<a name="1041693">
Let's look at one of these definitions in detail. Here's the new definition for the <code>Engineer</code> constructor:</a></p><p>
<a name="1039389">
<pre>function Engineer (name, projs, mach) {<br>    this.base = WorkerBee;<br>    this.base(name, "engineering", projs);<br>    this.projects = mach || "";<br>}</pre></a><a name="1039407">
Assume we create a new <code>Engineer</code> object as follows:</a></p><p>
<a name="1039480">
<pre>jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");</pre></a><a name="1039489">
JavaScript follows these steps:</a></p><p>
<a name="1039507">
<blockquote>1.&nbsp;&nbsp;&nbsp;First, the <code>new</code> operator creates a generic object and sets its <code>__proto__</code> property to <code>Engineer.prototype</code>.</blockquote>
</a>

<a name="1046227">
<blockquote>2.&nbsp;&nbsp;&nbsp;The <code>new</code> operator then passes the new object to the <code>Engineer</code> constructor as the value of the <code>this</code> keyword.</blockquote>
</a>

<a name="1039547">
<blockquote>3.&nbsp;&nbsp;&nbsp;Next, the constructor creates a new property called <code>base</code> for that object and assigns the value of the <code>WorkerBee</code> constructor to the <code>base</code> property. This makes the <code>WorkerBee</code> constructor a method of the <code>Engineer</code> object.</blockquote>
</a>

</p><blockquote><b>NOTE: </b><a name="1039614">
The name of the <code>base</code> property is not special. You can use any legal property 
name; <code>base</code> is simply evocative of its purpose.
</a></blockquote>

<a name="1039548">
<blockquote>4.&nbsp;&nbsp;&nbsp;Next, the constructor calls the <code>base</code> method, passing as its arguments two of the arguments passed to the constructor (<code>"Doe, Jane"</code> and <code>["navigator", "javascript"]</code>) and also the string <code>"engineering"</code>. Explicitly using <code>"engineering"</code> in the constructor indicates that all <code>Engineer</code> objects have the same value for the inherited <code>dept</code> property and this value overrides the value inherited from <code>Employee</code>.</blockquote>
</a>

<a name="1043716">
<blockquote>5.&nbsp;&nbsp;&nbsp;Because <code>base</code> is a method of <code>Engineer</code>, within the call to <code>base</code>, JavaScript binds the <code>this</code> keyword to the object created in step&nbsp;1. Thus, the <code>WorkerBee</code> function in turn passes the <code>"Doe, Jane"</code> and <code>["navigator", "javascript"]</code> arguments to the <code>Employee</code> constructor function. Upon return from the <code>Employee</code> constructor function, the <code>WorkerBee</code> function uses the remaining argument to set the <code>projects</code> property.</blockquote>
</a>

<a name="1039569">
<blockquote>6.&nbsp;&nbsp;&nbsp;Upon return from the <code>base</code> method, the <code>Engineer</code> constructor initializes the object's <code>machine</code> property to <code>"belau"</code>.</blockquote>
</a>

<a name="1039581">
<blockquote>7.&nbsp;&nbsp;&nbsp;Upon return from the constructor, JavaScript assigns the new object to the <code>jane</code> variable.</blockquote>
</a>

<a name="1043735">
You might think that, having called the <code>WorkerBee</code> constructor from inside the <code>Engineer</code> constructor, you've set up inheritance appropriately for <code>Engineer</code> objects. This is not the case. Calling the <code>WorkerBee</code> constructor ensures that an <code>Engineer</code>
object starts out with the properties specified in all constructor
functions that are called. However, if you later add properties to the <code>Employee</code> or <code>WorkerBee</code> prototypes, those properties are not inherited by the <code>Engineer</code> object. For example, assume you have these statements:</a><p>
<a name="1043743">
<pre>function Engineer (name, projs, mach) {<br>    this.base = WorkerBee;<br>    this.base(name, "engineering", projs);<br>    this.projects = mach || "";<br>}<br>jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");<br>Employee.prototype.specialty = "none";</pre></a><a name="1039690">
The <code>jane</code> object does not inherit the <code>specialty</code>
property. You still need to explicitly set up the prototype to ensure
dynamic inheritance. Assume instead you have these statements:</a></p><p>
<a name="1043797">
<pre>function Engineer (name, projs, mach) {<br>    this.base = WorkerBee;<br>    this.base(name, "engineering", projs);<br>    this.projects = mach || "";<br>}<br><b>Engineer.prototype = new WorkerBee;<br></b>jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");<br>Employee.prototype.specialty = "none";</pre></a><a name="1039736">
Now the value of the <code>jane</code> object's <code>specialty</code> property is <code>"none"</code>.</a></p><p>

<a name="Property Inheritance Revisited"></a>
<a name="1040208">
<h2> Property Inheritance Revisited</h2>
</a>
<a name="1040213">
The preceding sections have described how constructors and prototypes
provide hierarchies and inheritance in JavaScript. As with all
languages, there are some subtleties that were not necessarily apparent
in these earlier discussions. This section discusses some of those
subtleties.</a></p><p>

<a name="Head2;"></a>
<a name="1039092">
<h3> Local versus Inherited Values</h3>
</a>

<a name="1039778">
Let's revisit property inheritance briefly. As discussed earlier, when
you access an object property, JavaScript performs these steps:</a></p><p>
</p><ol>
<p>
<a name="1039805">
</a></p><li><a name="1039805">Check to see if the value exists locally. If it does, return that value. 
</a>

<p>
<a name="1039812">
</a></p></li><li><a name="1039812">If there isn't a local value, check the prototype chain (using the <code>__proto__</code> property). 
</a>

<p>
<a name="1039824">
</a></p></li><li><a name="1039824">If an object in the prototype chain has a value for the specified property, return that value. 
</a>

<p>
<a name="1039835">
</a></p></li><li><a name="1039835">If no such property is found, the object does not have the property. 
</a>

</li></ol>
<a name="1039784">
The outcome of this simple set of steps depends on how you've defined
things along the way. In our original example, we had these definitions:</a><p>
<a name="1039845">
<pre>function Employee () {<br>    this.name = "";<br>    this.dept = "general";<br>}</pre></a><a name="1039850">
<pre>function WorkerBee () {<br>    this.projects = [];<br>}<br>WorkerBee.prototype = new Employee;</pre></a><a name="1039842">
With these definitions, assume you create <code>amy</code> as an instance of <code>WorkerBee</code> with this statement:</a></p><p>
<a name="1043601">
<pre>amy = new WorkerBee;</pre></a><a name="1043596">
The <code>amy</code> object has one local property, <code>projects</code>. The values for the <code>name</code> and <code>dept</code> properties are not local to <code>amy</code> and so are gotten from the <code>amy</code> object's <code>__proto__</code> property. Thus, <code>amy</code> has these property values:</a></p><p>
<a name="1040019">
<pre>amy.name == "";<br>amy.dept = "general";<br>amy.projects == [];</pre></a><a name="1040018">
Now assume you change the value of the <code>name</code> property in the prototype associated with <code>Employee</code>:</a></p><p>
<a name="1040014">
<pre>Employee.prototype.name = "Unknown"</pre></a><a name="1040013">
At first glance, you might expect that new value to propagate down to all the instances of <code>Employee</code>. However, it does not. </a></p><p>
<a name="1043836">
When you create <i>any</i> instance of the <code>Employee</code> object, that instance gets a local value for the <code>name</code> property (the empty string). This means that when you set the <code>WorkerBee</code> prototype by creating a new <code>Employee</code> object, <code>WorkerBee.prototype</code> has a local value for the <code>name</code> property. Therefore, when JavaScript looks up the <code>name</code> property of the <code>amy</code> object (an instance of <code>WorkerBee</code>), JavaScript finds the local value for that property in <code>WorkerBee.prototype</code>. It therefore does not look farther up the chain to <code>Employee.prototype</code>.</a></p><p>
<a name="1040130">
If you want to change the value of an object property at runtime and
have the new value be inherited by all descendants of the object, you
cannot define the property in the object's constructor function.
Instead, you add it to the constructor's associated prototype. For
example, assume you change the code above to the following:</a></p><p>
<a name="1040189">
<pre>function Employee () {<br>&nbsp;&nbsp;&nbsp;this.dept = "general";<br>}<br>Employee.prototype.name = "";</pre></a><a name="1040190">
<pre>function WorkerBee () {<br>    this.projects = [];<br>}<br>WorkerBee.prototype = new Employee;</pre></a><a name="1040191">
<pre>amy = new WorkerBee;</pre></a><a name="1040207">
<pre>Employee.prototype.name = "Unknown";</pre></a><a name="1040183">
In this case, the <code>name</code> property of <code>amy</code> becomes <code>"</code>Unknown<code>"</code>.</a></p><p>
<a name="1047098">
As these examples show, if you want to have default values for object
properties and you want to be able to change the default values at
runtime, you should set the properties in the constructor's prototype,
not in the constructor function itself.</a></p><p>

<a name="Head2;"></a>
<a name="1039116">
<h3> Determining Instance Relationships</h3>
</a>

<a name="1043907">
You may want to know what objects are in the prototype chain for an
object, so that you can tell from what objects this object inherits
properties. In a class-based language, you might have an <code>instanceof</code> operator for this purpose. JavaScript does not provide <code>instanceof</code>, but you can write such a function yourself.</a></p><p>
<a name="1044344">
As discussed in </a><a href="http://www.cs.rit.edu/%7Eatk/JavaScript/manuals/jsobj/index.htm#1042848">"Inheriting Properties"</a>, when you use the <code>new</code> operator with a constructor function to create a new object, JavaScript sets the <code>__proto__</code> property of the new object to the value of the <code>prototype</code> property of the constructor function. You can use this to test the prototype chain.</p><p>
<a name="1044353">
For example, assume you have the same set of definitions we've been using, with the prototypes set appropriately. Create an <code>__proto__</code> object as follows:</a></p><p>
<a name="1044359">
<pre>chris = new Engineer("Pigman, Chris", ["jsd"], "fiji");</pre></a><a name="1040250">
With this object, the following statements are all true:</a></p><p>
<a name="1044375">
<pre>chris.__proto__ == Engineer.prototype;<br>chris.__proto__.__proto__ == WorkerBee.prototype;<br>chris.__proto__.__proto__.__proto__ == Employee.prototype;<br>chris.__proto__.__proto__.__proto__.__proto__ == Object.prototype;<br>chris.__proto__.__proto__.__proto__.__proto__.__proto__ == null;</pre></a><a name="1044416">
Given this, you could write an <code>instanceOf</code> function as follows:</a></p><p>
<a name="1045649">
<pre>function instanceOf(object, constructor) { <br>&nbsp;&nbsp;&nbsp;while (object != null) { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (object == constructor.prototype) <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;object = object.__proto__; <br>&nbsp;&nbsp;&nbsp;} <br>&nbsp;&nbsp;&nbsp;return false; <br>}</pre></a><a name="1044450">
With this definition, the following expressions are all true:</a></p><p>
<a name="1044472">
<pre>instanceOf (chris, Engineer)<br>instanceOf (chris, WorkerBee)<br>instanceOf (chris, Employee)<br>instanceOf (chris, Object)</pre></a><a name="1044483">
But this expression is false:</a></p><p>
<a name="1044485">
<pre>instanceOf (chris, SalesPerson)</pre></a>
<a name="Head2;"></a>
<a name="1044473">
<h3> Global Information in Constructors</h3>
</a>

<a name="1043952">
When you create constructors, you need to be careful if you set global
information in the constructor. For example, assume that you want a
unique ID to be automatically assigned to each new employee. You could
use this definition for <code>Employee</code>:</a></p><p>
<a name="1044006">
<pre>var idCounter = 1;</pre></a><a name="1044007">
<pre>function Employee (name, dept) {<br>&nbsp;&nbsp;&nbsp;this.name = name || "";<br>&nbsp;&nbsp;&nbsp;this.dept = dept || "general";<br>&nbsp;&nbsp;&nbsp;this.id = idCounter++;<br>}</pre></a><a name="1044018">
With this definition, when you create a new <code>Employee</code>, the constructor assigns it the next ID in sequence and then increments the global ID counter. So, if your next statement were:</a></p><p>
<a name="1044030">
<pre>victoria = new Employee("Pigbert, Victoria", "pubs")<br>harry = new Employee("Tschopik, Harry", "sales")</pre></a><a name="1044028">
<code>victoria.id</code> is 1 and <code>harry.id</code> is 2. At first glance that seems fine. However, <code>idCounter</code> gets incremented every time an <code>Employee</code> object is created, for whatever purpose. If you create the entire <code>Employee</code> hierarchy we've been working with, the <code>Employee</code> constructor is called every time we set up a prototype. That is, assume you have this code:</a></p><p>
<a name="1044070">
<pre>var idCounter = 1;</pre></a><a name="1044071">
<pre>function Employee (name, dept) {<br>&nbsp;&nbsp;&nbsp;this.name = name || "";<br>&nbsp;&nbsp;&nbsp;this.dept = dept || "general";<br>&nbsp;&nbsp;&nbsp;this.id = idCounter++;<br>}</pre></a><a name="1044074">
<pre>function Manager (name, dept, reports) {...}<br>Manager.prototype = new Employee;</pre></a><a name="1044082">
<pre>function WorkerBee (name, dept, projs) {...}<br>WorkerBee.prototype = new Employee;</pre></a><a name="1044094">
<pre>function Engineer (name, projs, mach) {...}<br>Engineer.prototype = new WorkerBee;</pre></a><a name="1044096">
</a></p><pre><a name="1044096">function SalesPerson (name, projs, quota) {...}<br>SalesPerson.prototype = new WorkerBee;</a></pre><a name="1044101">
<pre>mac = new Engineer("Wood, Mac");</pre></a><a name="1044063">
Further assume that the definitions we've omitted here have the <code>base</code> property and call the constructor above them in the prototype chain. In this case, by the time the <code>mac</code> object is created, <code>mac.id</code> is 5.</a><p>
<a name="1047015">
Depending on the application, it may or may not matter that the counter
has been incremented these extra times. If you care about the exact
value of this counter, one possible solution involves instead using
this constructor:</a></p><p>
<a name="1047016">
<pre>function Employee (name, dept) {<br>&nbsp;&nbsp;&nbsp;this.name = name || "";<br>&nbsp;&nbsp;&nbsp;this.dept = dept || "general";<br>&nbsp;&nbsp;&nbsp;if (name)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this.id = idCounter++;<br>}</pre></a><a name="1047017">
When you create an instance of <code>Employee</code>
to use as a prototype, you do not supply arguments to the constructor.
Using this definition of the constructor, when you do not supply
arguments, the constructor does not assign a value to the id and does
not update the counter. Therefore, for an <code>Employee</code> to get an assigned id, you must specify a name for the employee. In our example, <code>mac.id</code> would be 1.</a></p><p>

<a name="Head2;"></a>
<a name="1039110">
<h3> No Multiple Inheritance</h3>
</a>

<a name="1044501">
Some object-oriented languages allow multiple inheritance. That is, an
object can inherit the properties and values from unrelated parent
objects. JavaScript does not support multiple inheritance.</a></p><p>
<a name="1044514">
As we've already said, inheritance of property values occurs at runtime
by JavaScript searching the prototype chain of an object to find a
value. Because an object has a single associated prototype, JavaScript
cannot dynamically inherit from more than one prototype chain.</a></p><p>
<a name="1044518">
In JavaScript you can have a constructor function call more than one
other constructor function within it. This gives the illusion of
multiple inheritance. For example, consider the following statements:</a></p><p>
<a name="1044499">
<pre>function Hobbyist (hobby) {<br>&nbsp;&nbsp;&nbsp;this.hobby = hobby || "scuba";<br>}</pre></a><a name="1044521">
<pre>function Engineer (name, projs, mach, hobby) {<br>&nbsp;&nbsp;&nbsp;this.base1 = WorkerBee;<br>&nbsp;&nbsp;&nbsp;this.base1(name, "engineering", projs);<br>&nbsp;&nbsp;&nbsp;this.base2 = Hobbyist;<br>&nbsp;&nbsp;&nbsp;this.base2(hobby);<br>&nbsp;&nbsp;&nbsp;this.projects = mach || "";<br>}<br>Engineer.prototype = new WorkerBee;</pre></a><a name="1044534">
<pre>dennis = new Engineer("Doe, Dennis", ["collabra"], "hugo")</pre></a><a name="1044497">
Further assume that the definition of <code>WorkerBee</code> is as we've previously seen it. In this case, the dennis object has these properties:</a></p><p>
<a name="1044543">
<pre>dennis.name == "Doe, Dennis"<br>dennis.dept == "engineering"<br>dennis.projects == ["collabra"]<br>dennis.machine == "hugo"<br>dennis.hobby == "scuba"</pre></a><a name="1044547">
So <code>dennis</code> does get the <code>hobby</code> property from the <code>Hobbyist</code> constructor. However, assume you then add a property to the <code>Hobbyist</code> constructor's prototype:</a></p><p>
<a name="1044565">
<pre>Hobbyist.prototype.equipment = ["mask", "fins", "regulator", "bcd"]</pre></a><a name="1044579">
The <code>dennis</code> object does not inherit this new property.</a></p><p>


</p>
<hr size="4">

<p> </p><center>Originally from:
<a href="http://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Class-Based_vs._Prototype-Based_Languages" target="_top">Mozilla Developer Center</a>
</center>
<p>
</p><p>
</p></body></html>
