Employee class could represent the set of all employees. An instance, on the other hand, is the instantiation of a class; that is, one of its members. For example, Victoria could be an instance of the Employee
class, representing a particular individual as an employee. An instance
has exactly the properties of its parent class (no more, no less).Table 1 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.
Table 1 Comparison of class-based (Java) and prototype-based (JavaScript) object systems
The Employee Example
The rest of this paper works with the simple employee hierarchy shown in Figure 1.
Figure 1 A simple object hierarchy
Employee has the properties name (whose value defaults to the empty string) and dept (whose value defaults to "general").Manager is based on Employee. It adds the reports property (whose value defaults to an empty array, intended to have an array of Employee objects as its value).WorkerBee is also based on Employee. It adds the projects property (whose value defaults to an empty array, intended to have an array of strings as its value).SalesPerson is based on WorkerBee. It adds the quota property (whose value defaults to 100). It also overrides the dept property with the value "sales", indicating that all salespersons are in the same department.Engineer is based on WorkerBee. It adds the machine property (whose value defaults to the empty string) and also overrides the dept property with the value "engineering".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. Figure 2 illustrates the hierarchy with these simple definitions.
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 "More Flexible Constructors". For now, these simple definitions let us look at how the inheritance occurs.
Figure 2 What the definitions look like
Using these definitions, you can create instances of these objects that get the default values for their properties. Figure 3 illustrates using these JavaScript definitions to create new objects and shows the property values for the new objects.
NOTE: As described earlier, the term instance 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 thatFigure 3 Creating objects with the simple definitionsjaneis an instance ofEngineer. Similarly, although the terms parent, child, ancestor, and descendant do not have formal meanings in JavaScript, we can use them informally to refer to objects higher or lower in the prototype chain.
Object Properties
This section discusses how objects inherit properties from other
objects in the prototype chain and what happens when you add a property
at runtime.
Inheriting Properties
Assume you create the mark object as a WorkerBee as shown in Figure 3 with this statement:
mark = new WorkerBee;
When JavaScript sees the new operator, it creates a new generic object and passes this new object as the value of the this keyword to the WorkerBee constructor function. The constructor function explicitly sets the value of the projects property. It also sets the value of the internal __proto__ property to the value of WorkerBee.prototype. (That property name has 2 underscore characters at the front and 2 at the end.) The __proto__
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 mark to that object.
mark.name = "";
The
mark.dept = "general";
mark.projects = [];mark object inherits values for the name and dept properties from the prototypical object in mark.__proto__. It is assigned a local value for the projects property by the WorkerBee
constructor. Simply put, this gives you inheritance of properties and
their values in JavaScript. Some subtleties of this process are
discussed in "Property Inheritance Revisited".
mark.name = "Doe, Mark";
mark.dept = "admin";
mark.projects = ["navigator"]; Adding Properties
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:
mark.bonus = 3000;
Now, the mark object has a bonus property, but no other WorkerBee has this property.
Employee.prototype.specialty = "none";
As soon as JavaScript executes this statement, the mark object also has the specialty property with the value of "none". Figure 4 shows the effect of adding this property to the Employee prototype and then overriding it for the Engineer prototype.
More Flexible Constructors
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. Figure 5 shows one way to do this.
Figure 5 Specifying properties in a constructor, take 1
Here are the Java and JavaScript definitions for these objects.
These JavaScript definitions use a special idiom for setting default values:
this.name = name || "";
The JavaScript logical OR operator (||)
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 name has a useful value for the name property. If it does, it sets this.name to that value. Otherwise, it sets this.name to the empty string. This paper uses this idiom for brevity; however, it can be puzzling at first glance.
With these definitions, when you create an instance of an object, you
can specify values for the locally defined properties. As shown in Figure 5, you can use this statement to create a new Engineer:
jane = new Engineer("belau");
Jane's properties are now:
jane.name == "";
Notice that with these definitions, you cannot specify an initial value for an inherited property such as
jane.dept == "general";
jane.projects == [];
jane.machine == "belau"name.
If you want to specify an initial value for inherited properties in
JavaScript, you need to add more code to the constructor function.
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. Figure 6 shows these new definitions.
Figure 6 Specifying properties in a constructor, take 2
function Engineer (name, projs, mach) {
Assume we create a new
this.base = WorkerBee;
this.base(name, "engineering", projs);
this.projects = mach || "";
}Engineer object as follows:
jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");
JavaScript follows these steps:
1. First, the
new operator creates a generic object and sets its __proto__ property to Engineer.prototype.2. The
new operator then passes the new object to the Engineer constructor as the value of the this keyword.3. Next, the constructor creates a new property called
base for that object and assigns the value of the WorkerBee constructor to the base property. This makes the WorkerBee constructor a method of the Engineer object.
NOTE: The name of thebaseproperty is not special. You can use any legal property name;baseis simply evocative of its purpose.
4. Next, the constructor calls thebasemethod, passing as its arguments two of the arguments passed to the constructor ("Doe, Jane"and["navigator", "javascript"]) and also the string"engineering". Explicitly using"engineering"in the constructor indicates that allEngineerobjects have the same value for the inheriteddeptproperty and this value overrides the value inherited fromEmployee.
5. Becausebaseis a method ofEngineer, within the call tobase, JavaScript binds thethiskeyword to the object created in step 1. Thus, theWorkerBeefunction in turn passes the"Doe, Jane"and["navigator", "javascript"]arguments to theEmployeeconstructor function. Upon return from theEmployeeconstructor function, theWorkerBeefunction uses the remaining argument to set theprojectsproperty.
6. Upon return from thebasemethod, theEngineerconstructor initializes the object'smachineproperty to"belau".
7. Upon return from the constructor, JavaScript assigns the new object to the jane variable.
You might think that, having called the WorkerBee constructor from inside the Engineer constructor, you've set up inheritance appropriately for Engineer objects. This is not the case. Calling the WorkerBee constructor ensures that an Engineer
object starts out with the properties specified in all constructor
functions that are called. However, if you later add properties to the Employee or WorkerBee prototypes, those properties are not inherited by the Engineer object. For example, assume you have these statements:
function Engineer (name, projs, mach) {
The
this.base = WorkerBee;
this.base(name, "engineering", projs);
this.projects = mach || "";
}
jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");
Employee.prototype.specialty = "none";jane object does not inherit the specialty
property. You still need to explicitly set up the prototype to ensure
dynamic inheritance. Assume instead you have these statements:
function Engineer (name, projs, mach) {
Now the value of the
this.base = WorkerBee;
this.base(name, "engineering", projs);
this.projects = mach || "";
}
Engineer.prototype = new WorkerBee;
jane = new Engineer("Doe, Jane", ["navigator", "javascript"], "belau");
Employee.prototype.specialty = "none";jane object's specialty property is "none".
Property Inheritance Revisited
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.
Local versus Inherited Values
Let's revisit property inheritance briefly. As discussed earlier, when
you access an object property, JavaScript performs these steps:
__proto__ property).
function Employee () {
this.name = "";
this.dept = "general";
}function WorkerBee () {
With these definitions, assume you create
this.projects = [];
}
WorkerBee.prototype = new Employee;amy as an instance of WorkerBee with this statement:
amy = new WorkerBee;
The amy object has one local property, projects. The values for the name and dept properties are not local to amy and so are gotten from the amy object's __proto__ property. Thus, amy has these property values:
amy.name == "";
Now assume you change the value of the
amy.dept = "general";
amy.projects == [];name property in the prototype associated with Employee:
Employee.prototype.name = "Unknown"
At first glance, you might expect that new value to propagate down to all the instances of Employee. However, it does not.
function Employee () {
this.dept = "general";
}
Employee.prototype.name = "";function WorkerBee () {
this.projects = [];
}
WorkerBee.prototype = new Employee;amy = new WorkerBee;
Employee.prototype.name = "Unknown";
In this case, the name property of amy becomes "Unknown".
Determining Instance Relationships
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 instanceof operator for this purpose. JavaScript does not provide instanceof, but you can write such a function yourself.
As discussed in "Inheriting Properties", when you use the new operator with a constructor function to create a new object, JavaScript sets the __proto__ property of the new object to the value of the prototype property of the constructor function. You can use this to test the prototype chain.
chris = new Engineer("Pigman, Chris", ["jsd"], "fiji");
With this object, the following statements are all true:
chris.__proto__ == Engineer.prototype;
Given this, you could write an
chris.__proto__.__proto__ == WorkerBee.prototype;
chris.__proto__.__proto__.__proto__ == Employee.prototype;
chris.__proto__.__proto__.__proto__.__proto__ == Object.prototype;
chris.__proto__.__proto__.__proto__.__proto__.__proto__ == null;instanceOf function as follows:
function instanceOf(object, constructor) {
With this definition, the following expressions are all true:
while (object != null) {
if (object == constructor.prototype)
return true;
object = object.__proto__;
}
return false;
}
instanceOf (chris, Engineer)
But this expression is false:
instanceOf (chris, WorkerBee)
instanceOf (chris, Employee)
instanceOf (chris, Object)
instanceOf (chris, SalesPerson)
Global Information in Constructors
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 Employee:
var idCounter = 1;
function Employee (name, dept) {
With this definition, when you create a new
this.name = name || "";
this.dept = dept || "general";
this.id = idCounter++;
}Employee, the constructor assigns it the next ID in sequence and then increments the global ID counter. So, if your next statement were:
victoria = new Employee("Pigbert, Victoria", "pubs")
harry = new Employee("Tschopik, Harry", "sales")victoria.id is 1 and harry.id is 2. At first glance that seems fine. However, idCounter gets incremented every time an Employee object is created, for whatever purpose. If you create the entire Employee hierarchy we've been working with, the Employee constructor is called every time we set up a prototype. That is, assume you have this code:
var idCounter = 1;
function Employee (name, dept) {
this.name = name || "";
this.dept = dept || "general";
this.id = idCounter++;
}function Manager (name, dept, reports) {...}
Manager.prototype = new Employee;function WorkerBee (name, dept, projs) {...}
WorkerBee.prototype = new Employee;function Engineer (name, projs, mach) {...}
Engineer.prototype = new WorkerBee;
function SalesPerson (name, projs, quota) {...}
SalesPerson.prototype = new WorkerBee;
mac = new Engineer("Wood, Mac");
Further assume that the definitions we've omitted here have the base property and call the constructor above them in the prototype chain. In this case, by the time the mac object is created, mac.id is 5.
function Employee (name, dept) {
When you create an instance of
this.name = name || "";
this.dept = dept || "general";
if (name)
this.id = idCounter++;
}Employee
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 Employee to get an assigned id, you must specify a name for the employee. In our example, mac.id would be 1.
No Multiple Inheritance
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.
function Hobbyist (hobby) {
this.hobby = hobby || "scuba";
}function Engineer (name, projs, mach, hobby) {
this.base1 = WorkerBee;
this.base1(name, "engineering", projs);
this.base2 = Hobbyist;
this.base2(hobby);
this.projects = mach || "";
}
Engineer.prototype = new WorkerBee;dennis = new Engineer("Doe, Dennis", ["collabra"], "hugo")
Further assume that the definition of WorkerBee is as we've previously seen it. In this case, the dennis object has these properties:
dennis.name == "Doe, Dennis"
So
dennis.dept == "engineering"
dennis.projects == ["collabra"]
dennis.machine == "hugo"
dennis.hobby == "scuba"dennis does get the hobby property from the Hobbyist constructor. However, assume you then add a property to the Hobbyist constructor's prototype:
Hobbyist.prototype.equipment = ["mask", "fins", "regulator", "bcd"]
The dennis object does not inherit this new property.