A. As in most object-oriented, event-driven
programming languages, there are four distinct entities in
JavaScript:
OBJECTS. A discussion of objects
is beyond the scope of this FAQ (see the section "Objects
and the Web" in IDM's Intranet
FAQ for background). It's impossible to understand JavaScript
without knowing the following essentials, however:
everything you can control in a web browser is an object
comprising properties and methods (sometimes
referred to in the literature as attributes and operations,
respectively)
properties define the state of an object; e.g., red
text, 10-element array
methods define the actions that change the state
of an object; e.g., fontcolor("red")
sets the color of a text object to red.
FUNCTIONS. Methods that operate
outside of objects; e.g., escape() and unescape(),
JavaScript functions that perform ASCII to hex conversions. The
existence of non-object-specific functions in JavaScript
keeps it from being a truly object-oriented language like Java.
STATEMENTS. Programming commands
that control object lifecycles and the flow of execution; e.g.,
if..else, while. JavaScript statements
and syntax closely resemble those of the 'C' programming language.
EVENTS. Things that happen, usually
as a result of user actions, to which a JavaScript program can
respond; e.g., a mouse click. Events always happen in relation
to a given object, such as a button in a form (for which onClick
is a typical event), or an entire web page (sample event: onLoad).
The code that specifies what the object should do in response
to an event is a special type of method called an event handler.
Q. How does JavaScript model the world?
A. This question goes to the heart of any OOPS [Object-Oriented
Programming System]. The abbreviated answer given here -- lengthy
as it is -- omits important differences between the Netscape and
Microsoft broswer object models, and between various versions of
JavaScript. For a more accurate discussion refer to one of the programmer's
references cited elsewhere in this FAQ.
A. Among other things, event handlers are the subject
of part II of IDM's 3-part tutorial, JavaScript
101, by Reaz Hoque. That's a good starting place. Also refer
to the following diagram from Wrox Press Ltd., which puts browser
events and the code that handles them in context.
A. The languages have enough in common to make learning
one easy if you know the other. By the same token, the differences
are subtle enough to trip up those proficient in both. Here's a
short list comparing C and JavaScript:
Terminating JavaScript command lines in semicolons is
optional; in C it's mandatory. Recommended practice is to use
them religiously in both languages (and Java as well).
Both JavaScript and C are case-sensitive; 'doThis' is
different from 'DOTHIS'. Experienced programmers learn to love
this feature, which drives beginners nuts.
Both JavaScript and C are block-structured computer languages
and employ curly brackets -- '{' and '}' -- to delimit blocks.
Both JavaScript and C employ quotation -- enclosure in
single or double quote marks -- to designate text strings.
Arrays in both JavaScript and C are zero-based; the first
element is myArray[0], not myArray[1].
Both JavaScript and C employ '==' for comparison, '=' for equality,
and '!' for negation. In fact the set of JavaScript operators
is essentially borrowed from C (right down to the deprecated ternary
construct a ? b : c).
Both JavaScript and C employ the symbols /* to designate a comment
*/. JavaScript also permits the use of '//' for short comments,
as in C++.
Finally, JavaScript's statements are a strict subset of C++'s,
offering a smaller selection of identical looping and conditional
constructs.