Answers
to Questions about... the First Principles of JavaScript/JScript
1. What are the language's basic entities?
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 Intranet Journal'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.