JavaScript – Objects

JavaScript’s fundamental datatype is the
object. An object is a composite value: it
aggregates multiple values (primitive values or other objects) and
allows you to store and retrieve those values by name. An object is an
unordered collection of properties, each of which
has a name and a value. Property names are strings, so we can say that
objects map strings to values. This string-to-value mapping goes by
various names: you are probably already familiar with the fundamental
data structure under the name “hash,” “hashtable,” “dictionary,” or
“associative array.” An object is more than a simple string-to-value
map, however. In addition to maintaining its own set of properties, a
JavaScript object also inherits the properties of another object, known
as its “prototype.” The methods of an object are typically inherited
properties, and this “prototypal inheritance” is a key feature of
JavaScript.

JavaScript objects are dynamic—properties can usually be added and
deleted—but they can be used to simulate the static objects and
“structs” of statically typed languages. They can also be used (by
ignoring the value part of the string-to-value mapping) to represent
sets of strings.

Any value in JavaScript that is not a string, a number, true, false, null, or undefined is an object. And even though
strings, numbers, and booleans are not objects, they behave like
immutable objects (see Wrapper Objects).

Recall from Immutable Primitive Values and Mutable Object
References that objects
are mutable and are manipulated by reference rather
than by value. If the variable x
refers to an object, and the code var y =
x;
is executed, the variable y holds a reference to the same object, not a
copy of that object. Any modifications made to the object through the
variable y are also visible through
the variable x.

The most common things to do with objects are create them and to
set, query, delete, test, and enumerate their properties. These
fundamental operations are described in the opening sections of this
chapter. The sections that follow cover more advanced topics, many of
which are specific to ECMAScript 5.

A property has a name and a value. A property
name may be any string, including the empty string, but no object may
have two properties with the same name. The value may be any JavaScript
value, or (in ECMAScript 5) it may be a getter or a setter function (or
both). We’ll learn about getter and setter functions in Property Getters and Setters. In addition to its name and value, each
property has associated values that we’ll call property
attributes
:

  • The writable attribute specifies whether
    the value of the property can be set.

  • The enumerable attribute specifies
    whether the property name is returned by a for/in loop.

  • The configurable attribute specifies
    whether the property can be deleted and whether its attributes can
    be altered.

Prior to ECMAScript 5, all properties in objects created by your
code are writable, enumerable, and configurable. In ECMAScript 5, you
can configure the attributes of your properties. Property Attributes explains how to do this.

In addition to its properties, every object has three associated
object attributes:

  • An object’s prototype is a reference to
    another object from which properties are inherited.

  • An object’s class is a string that
    categorizes the type of an object.

  • An object’s extensible flag specifies (in
    ECMAScript 5) whether new properties may be added to the
    object.

We’ll learn more about prototypes and property inheritance in
Prototypes and Inheritance, and we
will cover all three attributes in more detail in Object Attributes.

Finally, here are some terms we’ll use to distinguish among three
broad categories of JavaScript objects and two types of
properties:

  • A native object is an object or class of
    objects defined by the ECMAScript specification. Arrays, functions,
    dates, and regular expressions (for example) are native
    objects.

  • A host object is an object defined by the
    host environment (such as a web browser) within which the JavaScript
    interpreter is embedded. The HTMLElement objects that represent the
    structure of a web page in client-side JavaScript are host objects.
    Host objects may also be native objects, as when the host
    environment defines methods that are normal JavaScript Function
    objects.

  • A user-defined object is any object
    created by the execution of JavaScript code.

  • An own property is a property defined
    directly on an object.

  • An inherited property is a property
    defined by an object’s prototype object.

Comments are closed.