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.
function prn() {
for(var i=0; i < arguments.length; )
document.write(arguments[i++]);
}
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.
function ob(a,b) {
this.a = a;
this.b = b;
this.prn = prn_ob;
return 'ob';
}
function prn_ob() {
document.write('prn: a=',
this.a, ' b=', this.b, '\n');
}
xx = new ob(3,4);
xx.prn();
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().
document.writeln("Please thank this site for adding a link to me!");
document.write(document.referrer);
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:
-
reverse(): reverses the index order and returns the reversed array.
-
concat(arg1 [,arg2,...]) returns the concatenation of the array
with the array or single valued arguments arg1, arg2, ....
-
join(str) returns a string consisting of the joined string representations
of all elements separated by the string argument str.
-
pop() removes the last element from the array and returns this element,
if the array was empty, it returns undefined.
-
shift() removes the first element from the array and returns this
element, if the array was empty, it returns undefined.
-
unshift(arg1 [, arg2, ..]) inserts the arguments arg1, arg2, ...
in front of the array and returns the length of the new array.
-
sort([comp]) sorts the array and returns the sorted array. The sorting
order can be specified by an comparision function whose reference is specified
as argument comp. If no argument is given the string representations of
the elements are used to order the elements in lexical order.