↑
Main Page
Object Basics
Object Basics
ECMAScript objects are one of the unique (and useful) features of JavaScript. Chapter 2,
“ECMAScript Basics,” introduced the concept that everything is an object, including functions.
This chapter focuses on how to manipulate and use those objects, as well as how to create your
own objects to add functionality specific to your needs.
Object-Oriented Terminology
ECMA-262 defines an
object
as an “unordered collection of properties each of which contains a
primitive value, object, or function.” Strictly speaking, this means that an object is an array of val-
ues in no particular order. Although this is ECMAScript’s interpretation, an
object
is more generi-
cally defined to be a code-based representation of a noun (person, place, or thing).
Each object is defined by a
class
, which can be thought of as a recipe for an object. The class defines
both the
interface
of an object (the properties and methods that can be accessed by developers) as
well as the inner workings of the object (the code that makes the properties and methods work).
The compiler or interpreter uses the class to build objects according to its specifications.
When a program uses a class to create an object, the resulting object is said to be an
instance
of the
class. The only limit to the number of instances that can be created from a single class is the physi-
cal memory limitations of the machine on which the code is running. Each instance behaves the
same way, but each can handle separate sets of data. The process of creating an object instance
from a class is called
instantiation
.
As I discussed briefly in Chapter 1, ECMAScript has no formal classes. Instead, ECMA-262 describes
object definitions
as the recipes for an object. This is a logical compromise for ECMAScript, because
object definitions actually are objects in and of themselves (which I explain shortly). Even though
classes don’t actually exist, this book refers to object definitions as classes because the term is more
familiar to most developers and, functionally, the two are equivalent.
06_579088 ch03.qxd 3/28/05 11:36 AM Page 67
Free JavaScript Editor
Ajax Editor
©
→ R7