HTML and CSS Reference
In-Depth Information
color: "Red",
seats: 5,
doors: 5,
accessories: ["Air condition", "Electric Windows"],
drive: function () {
Incidentally, Listing 7.1 shows a few other literals available in JavaScript as well,
most notably the array literal ( [] as opposed to new Array() ).
ECMA-262 defines a JavaScript object as an unordered collection of properties.
Properties consist of a name, a value, and a set of attributes. Property names are
either string literals, number literals, or identifiers. Properties may take any value,
i.e., primitives (strings, numbers, booleans, null or undefined ) and objects,
including functions. When properties have function objects assigned to them, we
usually refer to them as methods. ECMA-262 also defines a set of internal proper-
ties and methods that are not part of the language, but are used internally by the
implementation. The specification encloses names of these internal properties and
methods in double brackets, i.e., [[Prototype]]. I will use this notation as well.
7.1.1 Property Access
JavaScript properties can be accessed in one of two ways—using dot notation,
car.model.year , or using a style commonly associated with dictionaries or
hashes, car["model"]["year"] . The square bracket notation offers a great
deal of flexibility when looking up properties. It can take any string or expression
that returns a string. This means that you can dynamically figure out the property
name at run-time and look it up on an object directly using the square brackets.
Another benefit of the square bracket notation is that you can access properties
whose name contain characters not allowed in identifiers such as white space. You
can mix dot and bracket notation at will, making it very easy to dynamically look
up properties on an object.
As you might remember, we used property names containing spaces to make
our test case names more human-readable in Chapter 3, Tools of the Trade, as seen in
Listing 7.2.
Search WWH ::

Custom Search