
18
|
JavaScript Pocket Reference
while
The while statement is a basic loop. It repeatedly exe-
cutes a statement while an expression is
true:
while ( expression )
statement ;
with
The with statement adds an object to the scope chain, so
that a statement is interpreted in the context of the
object:
with ( object )
statement ;
The with statement has some complex and non-intuitive
side effects; its use is strongly discouraged.
Object-Oriented JavaScript
JavaScript objects are associative arrays that associate values
with named properties. JavaScript provides a simple inherit-
ance mechanism, and it is possible to define new classes of
objects for use in your own programs. To define a new class,
start by writing a constructor function. A constructor is like
any other function, except it is invoked with the
new opera-
tor and it uses the
this keyword to refer to and initialize the
newly created object. For example, here is a constructor to
create objects of a new class named
Point.
function Point(x,y) { // Constructor for Point
this.x = x; // Initialize X coordinate
this.y = y; // Initialize Y coordinate
}
Every JavaScript function used as a constructor has a property
named
prototype. This property refers to a special prototype
object for the class of objects created by the constructor. Any
properties you define on this prototype object are inherited by
all objects created with the constructor function. The proto-