5.7.3 “use strict”
5.8 Summary of JavaScript Statements
This chapter introduced each of the JavaScript language’s statements. Table 5-1 sum-marizes them, listing the syntax and purpose of each.
Table 5-1. JavaScript statement syntax
Statement Syntax Purpose
break break [label]; Exit from the innermost loop or switch or from named enclosing statement
case case expression: Label a statement within a switch
continue continue [label]; Begin next iteration of the innermost loop or the named loop
debugger debugger; Debugger breakpoint
default default: Label the default statement within a switch do/while do statement while (expression); An alternative to the while loop
empty ; Do nothing
for for(init; test; incr) statement An easy-to-use loop
for/in for (var in object) statement Enumerate the properties of object function function name([param[,...]]) { body } Declare a function named name if/else if (expr) statement1 [else statement2] Execute statement1 or statement2 label label: statement Give statement the name label return return [expression]; Return a value from a function
switch switch (expression) { statements } Multiway branch to case or default: labels
throw throw expression; Throw an exception
try try { statements }
[catch { handler statements }]
[finally { cleanup statements }]
Handle exceptions
Statement Syntax Purpose
use strict "use strict"; Apply strict mode restrictions to script or function var var name [ = expr] [ ,... ]; Declare and initialize one or more variables while while (expression) statement A basic loop construct
with with (object) statement Extend the scope chain (forbidden in strict mode)
5.8 Summary of JavaScript Statements | 113
Core JavaScript
CHAPTER 6
Objects
JavaScript’s fundamental datatype is the object. An object is a composite value: it ag-gregates 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 inheri-tance” 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 lan-guages. They can also be used (by ignoring the value part of the string-to-value map-ping) 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 §3.6).
Recall from §3.7 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 modifi-cations 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
115
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 §6.6. 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. §6.7 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 §6.1.3 and §6.2.2, and we will cover all three attributes in more detail in §6.8.
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 specifi-cation. 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.