Introduction to Javascript
Since all of the scripts are written using Javascript in todo4teams, here is a brief introduction to this programming language.
Declaration of Variables
With the declaration a variable in the current scope is created, it can be a global variable or local function variable. It can be installed either without value:
Or the initial value can be specified equal to:
Instead of the 32 any expression can be used. The scheme is:
Control Structures: Branching and Looping
Control structures are instructions, which in itself contain further blocks of instructions. This includes the conditional command, the so called if-command.
println("Volljährig!");
} else {
println("Noch nicht volljährig.");
}
Between the parentheses is one single expression and between the braces there are one or more commands.
With the switch-command you can make notes to branches, in which a value is compared with many other values and a corresponding command is executed.
Loops
Loops can be expressed in Javascript in various ways:
The while-loop
anweisungen;
}
or the for-loop:
anweisungen;
}
Functions
Functions are full-fledged objects in Javascript. They have properties and methods, can be edited or be overwritten, be given as arguments to functions or are generated and returned by them.
Not every argument of a function must be specified in the command, for missing arguments the value is set to undefined. There are different ways to create a function in Javascript. The easiest way is to declare:
anweisungen;
return ausdruck;
}
Operators
Here is an overview of the operators in Javascript:
Operator | Operand type | Operation performed |
* | Numbers | Multiplication |
/ | Numbers | Division |
+ | Numbers or strings | Addition or string-chain |
– | Numbers | Subtraction |
% | Numbers | Modulo-operator |
< | Numbers or strings | Smaller than |
<= | Numbers or strings | Smaller or equal |
> | Numbers or strings | Bigger than |
>= | Numbers or strings | Bigger or equal |
Any variable | Allocation | |
*= | Number-variable | Allocation with multiplication |
/= | Number-variable | Allocation with division |
%= | Number-variable | Allocation with modulo-operator |
+= | Number- or string-variable | Allocation with addition or string-chain |
–= | Number-variable | Allocation with subtraction |
<<= | Integer-variable | Allocation with left-shift |
>>>= | Integer-variable | Allocation with right-shift or zero-filling |
>>= | Integer-variable | Allocation with right-shift |
= | Elementary types | Equal; identical value |
≠ | Elementary types | Not Equal; non-identical value |
& | Integer | Bitwise AND |
| | Integer | Bitwise OR |
. | Object, property | Access to property |
[ ] | Array, integer or string | Access to array-element |
( ) | Function, any type of parameter | Function command |
++ | Number-variable | Increment |
–– | Number-variable | Decrement |
&& | Boolean values | Logical AND |
|| | Boolean values | Logical OR |
^ | Integer | Bitwise XOR |
~ | Integer | Bitwise NOT (bitwise complementary) |
>>> | Integer | Shift to the right with zero-filling |
>> | Integer | Left-shift |
<< | Integer | Right-shift |
Arrays
There are two ways in Javascript to create Arrays:
Generation by Literal Construction
The data-type must not be named. The Javascript-interpreter knows that the square brackets will create an array:
Generation by Array-Construktor
The difference with the first method is insignificant. Parentheses replace the square brackets and the array-constructor is explicitly listed.
Access to Array-Elements
A script can create an array as an empty array that will be filled later by the program with data.
var farben = new Array();
or
farben = [];
Access to the elements of the array takes part by the array name and an index. The following declarations initialize the array.
farben[0] = "rot";
farben[1] = "grün";
farben[2] = "blau";
Assigning a new value to an array changes elements:
farben[0] = "orange";
Determining the Size of the Array
The size of the array automatically evolves by the number of occupied elements. For numeric arrays the length property finds out how many elements the array contains: array.length.
array.length is always one more than the highest index. The safest way to include a new element in an array is:
var farben = ["rot", "grün", "blau"];
farben[farben.length] = "gelb";
Objects
Each object (a literal object created) inherits from the prototype of the global object constructor.
Predefined Objects
Javascript knows various implementeted objects:
- The nameless global object that contains all the variables and objects.
- Object as a general prototype of which all objects are derived.
- Function as a prototype for functions
- Array as a prototype for arrays
- String as a prototype for character strings
- Boolean as a prototype for Boolean variables
- Number as a prototype for numbers (64-bit floating point numbers according to IEEE 754)
- Math provides constants and methods for mathematical operations. Math can not be used as a constructor.
- Date for operations on data or points in time and date formats
- RegExp for regular expressions
In the server-sided Javascript used here also all objects can be accessed that are passed to the script in the context.
Access to Objectproperties and -methods
Properties of objects (methods are also properties) are addressed as follows:
Dot notation:
objekt.eigenschaft;
objekt.methode([Parameter]);
In addition to the dot notation there is the bracket notation, which is far less common.
Error Handling
The command try … catch … finally catches exceptions that occur because of an error or a throw-command. The syntax is as follows:
// Anweisungen, in denen Ausnahmen auftreten oder ausgelöst werden können
} catch (exception) {
// Anweisungsfolge, die im Ausnahmefall ausgeführt wird.
// In diesem Teil kann die Fehlerbehandlung erfolgen.
} finally {
// Anweisungsfolge, die anschließend in jedem Fall ausgeführt wird.
}
…
throw("sample exception");
At the start the commands in the try-block are executed. If an exception occurs, the control flow is diverted immediately to the catch-block with the exception-object as a parameter.
Normally the exception-block is skipped. After the execution of the try-block (even partially) and of the catch-block the commands in the finally-block are always executed. The finally-part can be omitted or alternatively the catch-part.