JavaScript

Web Sites

The best web site for detailed JavaScript info is at http://www.mpg-ana.uni-potsdam.de/local/js/2/base.html

Variables

Variables need not be declared before first use.
Primitive types are: boolean, number, undefined.
Reference types are: string, object, function.
You can get the type of a variable with 'typeof(a)' to find the type of 'a'.
All variables are global unless declared in a 'var' statment like this:  var b = 'hello';

Assignment of primative types copies values.  Assignment of reference types copies pointers.  That means that
    a = 'xxx';
    b = a;

has only one string in memory, but with two pointers to it.  This is particularly important for objects, and is one way that JavaScript (and Java) differ from C/C++.

Notice the complete lack of arrays.  You make them out of objects!

Functions

A function consists of a block of statements that can be executed by "calling" the function-name . Thus a function definition has the form
           function f(a,b) {
                document.write(a);
                document.write(b);
           }
And you can call the function like below.  If you do not supply enough arguments, the unfullfilled arguments become 'undefined'.
           f(4,'hello');
Function arguments are passed by value, not by reference.  So, how would you write a function that increments its argument?
Functions may be nested, in which case the inner function is local to the outer function.
Functions may be assigned to variables, and function variables may be used to call functions.  Functions may be passed as arguments to other functions.
    function f1(a) { document.write(a); }
    function f2(a) { document.write(a); }
    fvar = f1;
    fvar('1');
    fvar = f2;
    fvar('2');
It is possible in JavsScript to write a function that takes a variable number of arguments.  The idea is that the function is an object, and that the object has properties, and by inspecting the properties one can act upon an variable number of arguments.

Objects

Variables may be of type object.  All objects have a pointer to this within them.  The standard Java/C++ syntax of variable.property is used.  You can also use the syntax where property name can be computed at run time.
    ObjectName["PropertyName"]

Objects are created by the use of 'new', and any function name you want.  That function should use the 'this' pointer, and return the function itself.  This example should help.

Even weirder, once the object is created, you can add additional attributes at any time, just like this:  xx.c = 'Hello';.  Think of all the things you can do with dynamically creating new properties on existing objects!

All objects always have a valueOf() and toString() method.  What do you thing ob.toString() returns?

The most important object is document, a reference to the HTML file currently being examined.
Some properties include: bgColor(), lastModified(), referrer(), fgColor(), write(), and writeln().

 

Arrays

Arrays are objects.  You make an array by using the built in constructor 'Array'.
    a = new Array();
Array objects have the built in property 'length' which is always correct.  You can add additional properties any time you want.  You can assign to any spot in the array, and all arrays are dynamically dimentioned.  Arrays are zero based. What does the code below do?
    a[2] = 'ast';
    document.write(a.length);
    b = new Array();
    b = ['hello', [1,2,3]];
    document.write(b.length);

Arrays have the follwing built-in functions, and some others: