Main Page

Object class

This syntax is similar to Java, although ECMAScript requires parentheses to be used only if there are one
or more parameters. If there are no parameters, such as in the previous line of code, then the parentheses
can be safely omitted:
var o = new Object;
Chapter 3, “Object Basics,” contains a more in-depth look at objects and their behaviors. This section
focuses on those reference types that have primitive equivalents.
The Object class
The
Object
class itself isn’t very useful, but you should understand it before moving on to the other
classes. Why is that? Because the
Object
class in ECMAScript is similar to
java.lang.Object
in Java:
It is the base class from which all ECMAScript classes inherit. All the properties and methods of the
Object class are also present in the other classes, and so to understand the
Object
class is to understand
all the others better.
The
Object
class has the following properties:
?
constructor
— A reference value (pointer) to the function that created the object. For the
Object
class, this points to the native
Object()
function.
?
prototype
— A reference value to the object prototype for this object. Prototypes are discussed
further in Chapter 3. For the all classes, this returns an instance of
Object
by default.
The
Object
class also has several methods:
?
hasOwnProperty(
property
)
— Determines if a given property exists for the object. The
property must be specified as a string (for example,
o.hasOwnProperty(“name”)
).
?
isPrototypeOf(
object
)
— Determines if the object is a prototype of another object.
?
propertyIsEnumerable(
property
)
— Determines if a given property can be enumerated by
using the
for...in
statement (discussed later in this chapter).
?
toString()
— Returns a primitive string representation of the object. For the
Object
class,
this value is undefined in ECMA-262 and, as such, differs in each implementation.
?
valueOf()
— Returns the most appropriate primitive value of this object. For many classes,
this returns the same value as
toString()
.
Each of the properties and methods listed previously are designed to be overridden by other classes.
Although the parentheses aren’t required, it’s always best to include them in order
to avoid confusion.
26
Chapter 2
05_579088 ch02.qxd 3/28/05 11:35 AM Page 26


JavaScript EditorFree JavaScript Editor     Ajax Editor


©

R7