Friday, September 5, 2008

Introduction to JavaScript: A short glossary

statement
A statement in JavaScript is comparable to a sentence in English – both contain complete ideas
that the reader can understand independent of the rest of the document. A sentence is terminated
with a period, but a statement in JS always ends with a semicolon.

Example: x = 5+2;

variable
A storage container for values. Once a value has been assigned to a variable, that variable’s
identifier can be used in place of it’s value anywhere in a program. A variable’s value can be
changed at any point in the program (hence the name variable). Below myname is the identifier
and "Paul" is the value.

Example: var myname = “Paul”;

object
A part of the web page or browser that JavaScript can control. Primary objects in JavaScript
include:

windowThe browser window – has control over things like the status bar and scroll bars.
documentThe contents of the web page – has control over things like text color and size.

An object can contain other objects as properties.

property
All objects have a unique set of properties accessible to the programmer. Changing one or more
of these properties will change the way an object looks or behaves. In the following example,
window is the object and status is the property.

Example: window.status = “Hi there”;

Other examples of objects and properties:

ObjectProperty
document.bgColorChanging this changes the background color of the web page
window.locationChanging this loads a different page into the browser.


method
All objects also have a unique set of methods available to the programmer. Each method will
invoke some sort of action on the object. Methods are like functions (see the function handout) in
that you pass them parameters and they perform some action with those parameters, but they are
not user-defined like functions are. Rather, methods are built into the JavaScript language.

Example: window.alert(“This is an alert message”);

Other examples of objects and methods:

ObjectProperty
window.homt()This makes the browser load its user-defined homepage.
document.write(“bla”)This writes the text “bla” onto the webpage.


event handler
Many objects also have event handlers, which catch user triggered events (such as mouse clicks
and keystrokes) and allow the programmer to specify an action for each of these events.

Example: onClick= “alert(‘you clicked a button’);”

A few event handlers:
onLoadPerforms specified actions when page is loaded
onClickPerforms specified actions when mouse is clicked on the object that contains the handler statement.
onKeyPressDoes something when a key is pressed on the keyboard.