Isgn 890
Isgn 890
CHAPTER 3
■■■
W hen developing code with other programmers, which is standard for most corporate or
team projects, it becomes fundamentally important to maintain good authoring practices in
order to maintain your sanity. As JavaScript has begun to come into its own in recent years,
the amount of JavaScript code developed by professional programmers has increased dra-
matically. This shift in the perception and use of JavaScript has resulted in important
advances in the development practices surrounding it.
In this chapter, we’re going to look at a number of ways in which you can clean up your
code, organize it better, and improve the quality so that others can use it.
Prototypal Inheritance
JavaScript uses a unique form of object creation and inheritance called prototypal inheritance.
The premise behind this method (as opposed to the classical class/object scheme that most
programmers are familiar with) is that an object constructor can inherit methods from one
other object, creating a prototype object from which all other new objects are built.
This entire process is facilitated by the prototype property (which exists as a property
of every function, and since any function can be a constructor, it’s a property of them, too).
Prototypal inheritance is designed for single, not multiple, inheritance; however, there are
ways that this can be worked around, which I’ll discuss in the next section.
39
7273ch03final.qxd 11/16/06 8:21 AM Page 40
The part that makes this form of inheritance especially tricky to grasp is that prototypes
do not inherit their properties from other prototypes or other constructors; they inherit them
from physical objects. Listing 3-1 shows some examples of how exactly the prototype property
is used for simple inheritance.
The most important line in the previous example is User.prototype = new Person();.
Let’s look in depth at what exactly this means. User is a reference to the function constructor
of the User object. new Person() creates a new Person object, using the Person constructor.
You set the result of this as the value of the User constructor’s prototype. This means that any-
time you do new User(), the new User object will have all the methods that the Person object
had when you did new Person().
With this particular technique in mind, let’s look at a number of different wrappers that
developers have written to make the process of inheritance in JavaScript simpler.
Classical Inheritance
Classical inheritance is the form that most developers are familiar with. You have classes
with methods that can be instantiated into objects. It’s very typical for new object-oriented
JavaScript programmers to attempt to emulate this style of program design, however few
truly figure out how to do it correctly.
7273ch03final.qxd 11/16/06 8:21 AM Page 41
Thankfully, one of the masters of JavaScript, Douglas Crockford, set it as a goal of his
to develop a simple set of methods that can be used to simulate classlike inheritance
with JavaScript, as explained on his web site at http://javascript.crockford.com/
inheritance.html.
Listing 3-2 shows three functions that he built to create a comprehensive form of classi-
cal JavaScript inheritance. Each of the functions implement a different aspect of inheritance:
inheriting a single function, inheriting everything from a single parent, and inheriting indi-
vidual methods from multiple parents.
Listing 3-2. Douglas Crockford’s Three Functions for Simulating Classical-Style Inheritance Using
JavaScript
// Call the function to execute with all the arguments but the first
// (which holds the name of the function that we're executing)
ret = func.apply(this, Array.prototype.slice.apply(arguments, [1]));
return this;
});
return this;
});
Let’s look at what exactly these three functions provide us with and why we should use
them instead of attempting to write our own prototypal inheritance model. The premise for
the three functions is simple: