| Index: > A B C D E F G H I J K L M N O P Q R S T U V W X Y Z |
|
|||||
| First Prev [ 1 2 3 4 5 ] Next Last |
This loop goes through all properties of an object (or elements of an array).
for (variable in object) { statement }A function is a block with a (possibly empty) argument list that is normally given a name. A function may give back a return value.
function(arg1, arg2, arg3) { statements; return expression; }Example: Euclid's original algorithm of finding the greatest common divisor. (This is a geometrical solution which subtracts the longer segment from the shorter):
function gcd(segmentA, segmentB) { while(segmentA!=segmentB) if(segmentA>segmentB) segmentA -= segmentB; else segmentB -= segmentA; return(segmentA); }The number of arguments given when calling a function must not necessarily accord to the number of arguments in the function definition. Within the function the arguments may as well be accessed through the arguments array.
Every function is an instance of Function, a type of base object. Functions can be created and assigned like any other objects:
var myFunc1 = new Function("alert('Hello')"); var myFunc2 = myFunc1; myFunc2();results in the display:
HelloMost interaction with the user is done by using HTML form s which can be accessed through the HTML DOM. However there are as well some very simple means of communicating with the user:
Text elements may be the source of various events which can cause an action if a ECMAScript event handler is registered. In HTML these event handler functions are often defined as anonymous functions directly within the HTML tag.
List of events
1 Blur is when the object is deselected or something else is selected instead.
2 Focus is when the object is selected, usually in a form.
See also http://tech.irt.org/articles/js058/
Newer versions of JavaScript (as used in Internet Explorer 5 and Netscape 6) include a try ... catch error handling statement.
The try ... catch ... finally statement catches exceptions resulting from an error or a throw statement . Its syntax is as follows:
Initially, the statements within the try block execute. If an exception is thrown, the script's control flow immediately transfers to the statements in the catch block, with the exception available as the error argument. Otherwise the catch block is skipped. Once the catch block finishes, or the try block finishes with no exceptions thrown, the statements in the finally block execute. This figure summarizes the operation of a try...catch...finally statement:
Here's a script that shows try ... catch ... finally in action step by step.
The finally part may be omitted:
try { statements } catch (err) { // handle error }