Tutorial Appendices
Tutorial 15 - User-Defined Objects
The JavaScript tutorial appendix is a synopsis of important objects,
methods, properties, functions ,events and reserved words. It includes
a brief history of the JavaScript language.The terms used will help you
access an appropriate reference book that explains in detail what the
term means. Some useful references are:
- JavaScript Unleashed, Sams ['03]
- JavaScript Bible, IDG Books ['99]
|
- JavaScript - The Definitive Guide, O'Reilly ['01]
- Pure JavaScript, Sams ['99]
|
|---|
JavaScript Reserved Words
Reserved words are words with a special purpose. These words
cannot be used as variable names.
abstract boolean break byte case catch char
class const continue debugger default delete do
double else enum export extends false final
finally float for function goto if implements
import in instanceof int interface long native
new null package private protected public return
short static super switch synchronized this throw
throws transient true try typeof undefined var
void volatile while with
To avoid unnecessary debugging problems do not use any property
or method name as a variable name. Also avoid using a case
variant even when it is legal (eg. do not use instanceOf).
Client-Side Objects, Methods & Properties
Client-side objects are basic objects tied to a particular browser. This
may cause some inconsistencies in syntax or use as browser versions change.
Be aware that differences exist and always test your code. The
client-side objects are Document, Form, History, Location, Navigator
and Window.
- Document Object - Primary Output
- content properties - anchors[], applets[], embeds[], forms[],
images[], links [], plugins[]
- display properties - alinkColor, bgColor, fgColor, linkColor,
vlinkColor
- information properties - cookie, domain, lastModified, location,
mimeTypes, referrer, title, URL
- methods - open(), write(), writeln()
- Form Objects - Primary Input
- form properties - name, target, action, method, encoding, elements,
length
- form methods - submit(), reset()
- input, textarea and select methods - focus(), blur(), select()
- button, hidden, reset, and submit object properties - name, value, type
- checkbox properties - name, value, type, checked, default checked
- password properties - name, value, type, default value
- radio button properties - name, value, type, checked, default checked,
length
- select properties - name, value, type, length, options, defaultSelected,
index, selected, selectedIndex
- text, textarea properties - name, value, defaultvalue
- History Object - Access the browser's history file
- properties - length
- methods - back(), forward(), go()
- Location Object
- properties - hash, hostname, href, pathname, port, protocol, search
- methods - reload(), replace()
- Navigator Object - Root of all Objects
- properties - appCodeName, appName, appVersion, userAgent
- methods - javaEnabled()
- Window Object
- contained objects -
- Screen Object
- properties - availHeight, availLeft, availTop, availWidth, colorDepth,
height, pixelDepth, width
- properties - defaultStatus, frames, length, name, opener, parent,
self, status, top, window
- methods - alert(), blur(), clearInterval(), clearTimeout(), close(),
confirm(), focus(), open(), print(), prompt(), scroll(), setInterval(),
setTimeout(), timeoutID()
ECMA Core Objects, Methods & Properties
ECMA is a standards committee which defines specific core objects
(or internal constructs) to help a programmer. Some are used to access
operating system functions. Others are used to perform data and string
manipulations. The ECMA objects are Array, Boolean, Date, Error,
Function, Global, Math, Number, Object, RegExp and String
- Array Object
- properties - constructor, length, prototype
- methods - concat(), join(), pop(), push(), reverse(), shift(),
slice(), sort(), splice(), toLocaleString(), toString(), unshift(),
valueOf()
- Boolean Object
- properties - constructor, prototype
- methods - toString(), valueOf()
- Date Object
- properties - constructor, prototype
- methods - getDate(), getDay(), getFullYear(), getHours(),
getMilliseconds(), getMinutes(), getMonth(), getSeconds(),
getTime(), getTimeZoneOffset(), getUTCDate(), getUTCDay(),
getUTCFullYear(), getUTCHours(), getUTCMilliseconds(),
getUTCMinutes(), getUTCMonth(), getUTCSeconds(), getVarDate(),
getYear(), parse(), setDate(), setFullYear(), setHours(),
setMilliseconds(), setMinutes(), setMonth(), setSeconds(),
setTime(), setUTCDate(), setUTCFullYear(), setUTCHours(),
setUTCMilliseconds(), setUTCMinutes(), setUTCMonth(),
setUTCSeconds(), setUTCTime(), setUTCYear(), setYear(),
toLocaleString(), toUTCString(), toString(), UTC(), valueOf()
- Error Object
- properties - description, number
- methods -
- Function Object
- properties - arguments[], caller, constructor, prototype
- methods - toString(), valueOf()
- Global Object
- properties - Infinity, NaN, undefined
- methods - escape(), eval(), isFinite(), isNaN(), parseFloat(),
parseInt(), unescape()
- Math Object
- properties - E, LN10, LN2, LOG10E, LOG2E, PI, SQRT2, SQRT1_2
- methods - abs(), acos(), asin(), atan(), atan2(), ceil(), cos(),
exp(), floor(), log(), max(), min(), pow(), random(), round(), sin(),
sqrt(), tan()
- Number Object
- properties - MAX_VALUE, MIN_VALUE, NaN, NEGATIVE _INFINITY,
POSITIVE_INFINITY, constructor, prototype
- methods - toLocaleString(), toString(), valueOf()
- Object Object
- properties - constructor, prototype
- methods - toLocaleString(), toString(), valueOf()
- RegExp Object
- properties - $1 - $9, index, input, lastIndex, lastMatch, lastParen,
leftContext, rightContext
- String Object
- properties - constructor, length, prototype
- display methods - anchor(), big(), blink(), bold(), fixed(),
fontcolor(), fontsize(), italics(), small(), strike(), sub(), sup()
- manipulation methods - charAt(), charCodeAt(), concat(), fromCharCode(),
indexOf(), lastIndexOf(), link(), match(), replace(), search(), slice(),
split(), substr(), substring(), toLowerCase(), toUpperCase(),
toString(), valueOf()
JavaScript Events and Event Handlers
JavaScript events are external stimulae or actions by a user or
by the operating system that cause internal procedures to run which modify
the 'properties' of 'objects'. Clicking the mouse, pressing the [Enter] key,
and moving the cursor are some of the 'events' that start the ball rolling.
Event handlers are the specific methods for reacting to an event.
System Event Handlers:
| Event Name |
Handler Executes When |
| onError [window, image] |
window or image fails to load. |
| onLoad [window, frame, image] |
window, complete frame or image(s) finishes loading. |
| onUnload [window, frame] |
window or all windows in a frame have been exited. |
User Event Handlers:
NOTE: When used as an attribute, an event handler
must be typed in lowercase only (eg. onclick) to meet XHTML specifications!
Most, but not all of the following user event handlers can be
included as form control attributes.
| Event Name |
Handler Executes When |
| onAbort [image] |
image load has been abandoned by hitting the
STOP icon. |
| onBlur [window, frame, select, text, textarea] |
focus is lost (ie. changed or blurred)
to a new element. |
| onChange [select, text, textarea] |
last element has been changed before focus change. |
| onClick [clickable form element] |
user left mouse clicks or clicks on form element but can be stopped if
the executing procedure (such as validation) returns a false signal. |
| onContextmenu |
right mouse button clicked. |
| onDblclick |
mouse button is double-clicked. |
| onFocus [window, frame, select, text, textarea] |
object gains focus. aka anti-blur! |
| onHelp |
F1 button pressed. Used to override browser Help. |
| onKeydown |
alphanumeric key is pressed. |
| onKeypress |
alphanumeric key is fully pressed/released. |
| onKeyup |
alphanumeric key is released. |
| onMousedown |
either mouse button is clicked. |
| onMousemove |
mouse is moved. |
| onMouseOut [link, area] |
cursor leaves a link or area. |
| onMouseOver [link, area] |
cursor enters a link or area. |
| onReset [form] |
the reset button is clicked. |
| onResize |
a window is resized. |
| onSelect [text, textarea] |
some text is highlighted in either of these form boxes. |
| onStop |
GUI stop button is pressed or user leaves page. |
| onSubmit [form] |
Executes after a return key is pressed or a submit button is clicked
but allows a bailout similar to the onclick event. Failure in a validation
routine is the most common reason for bailout. |
In order to help the programmer, JavaScript includes several useful
built-in functions. Their names and usage follows:
| escape() |
Many special characters cause problems when submitting information to a
0CGI server. These characters include $ # ! spaces and tabs. An
example of a safely encoded string is coded = escape('& ').
[returns "%26%20"]. See also unescape. |
| eval() |
Evaluates a string and returns a numeric
value. An example is eval(x) + eval(y); If x and y are strings '1' and '2'
the result is three. Without the eval function the value would be 12! |
| isFinite() |
Tests whether the variable is a finite number. Returns false
if contents is not a number or is infinite. An example of usage is
flag = isFinite(var_name); |
| isNaN() |
Tests whether the variable is not a number. Returns true
if contents is not a decimal based number. An example of usage is
flag = isNaNum(var_name); |
| Number() |
Converts the object argument to a number representing the object's value.
If the value cannot be represented by a legitimate number, the "Not-a-Number"
value, NaN is returned. Note the inconsistency in case!! |
| parseFloat() |
Turns a string into a floating-point number. If the first
character can't be converted the result is NaN. Otherwise the conversion
stops at first character that can't be converted. The function is decimal
base only! an example of usage is n = parseFloat('23.45'); |
| parseInt() |
Turns a string into an integer number. If the first character
can't be converted the result is NaN. Otherwise the conversion stops at first
character that can't be converted. If a second parameter is included, the
numeric base can be binary, octal, decimal or hexadecimal. An example is
n = parseInt('55'); which places the number 55 into the variable n.
|
| unescape() |
Recovers an escaped string. An example is decoded =
unescape('%26%20') [returns "& "].
|
| new |
Creates a copy or instance of an object for use or
modification within a program. For example
now = new Date; creates a Date object
called now that can be affected by all the Date properties and
methods. Technically new is an operator but it works very much like a
function! |
| this |
A shorthand convention to allow working with the current object
method without naming it. This is used to save retyping a method name. |
| with |
A shorthand convention to allow working with the current object
without naming it. This is used to save retyping a long object name.
An example is:
with (document) {
writeln(lastmodified);
writeln(location);
writeln(title);
} |
A Brief History of JavaScript
Everything has a history and often knowing that history gives insight
into why things are done the way they are and what issued were solved
and what were missed or created. This topic attempts to reveal the
evolution of JavaScript.
Originally browsers only had one method of interactivity: forms
submitted to other sites. What was desperately required was interactivity
within the client browser itself. Netscape created a language called
LiveScript which was interpreted and provided the required
functionality for internal utilities and interaction. LiveScript was
rebranded as JavaScript to gain from the good publicity that
Java was getting. Its only commonality with Java is that both are OOP
languages that use C syntax as a starting point.
Successive versions have added functionality in response to user
demand. Image support, arrays, and plug-ins are examples of these additions.
MicroSoft implemented its version as JScript but unfortunately often
did things differently or included features not implemented in JavaScript.
This was part of the browser wars and caused much grief for program authors.
Finally in 1996, developers had had enough and asked for standards!
Unfortunately several standards group accepted the challenge. ECMA was the
most successful and now ECMAScript is available on most browsers. However,
ECMASCript is restricted to defining the core language. The browser
interface is standardized through the DOM recommendation of w3.org.
DOM also covers other languages that must interface with a browser,
not just JavaScript.
Tutorial 15 - User-Defined Objects
Copyright (c) 2005 by John W. M. Russell
|