Example:
function multiply(a, b) {
- return a * b
- }
+ return a * b
+ }
When encountering the `return` keyword further execution of the function is terminated and
the specified value is supplied to the caller of the function:
`multiply` function like this:
function multiply(a, b) {
- a * b
- }
+ a * b
+ }
Anonymous functions can be created by omitting the name in the function definition. The
resulting function object can be used like any other value:
var fn = function() { 3 }
-
- fn() /* Returns 3 */
+
+ fn() /* Returns 3 */
## <a id="lambdas"></a> Lambda Expressions
Multiple statements can be used by putting the function body into braces:
f = (x) => {
- log("Lambda called")
- x * x
+ log("Lambda called")
+ x * x
}
Just like with ordinary functions the return value is the value of the last statement.
object or apply statement. Local variables can be declared using the `var` keyword:
function multiply(a, b) {
- var temp = a * b
- return temp
- }
+ var temp = a * b
+ return temp
+ }
Each time the `multiply` function is invoked a new `temp` variable is used which is in no way
related to previous invocations of the function.
operates on.
object Host "localhost" {
- check_interval = 5m
+ check_interval = 5m
}
In this example the `this` scope refers to the "localhost" object. The `check_interval` attribute
You can explicitly access the `this` scope using the `this` keyword:
object Host "localhost" {
- var check_interval = 5m
+ var check_interval = 5m
/* This explicitly specifies that the attribute should be set
* for the host, if we had omitted `this.` the (poorly named)
a function is set to whichever object was used to invoke the function. Here's an example:
hm = {
- h_word = null
-
- function init(word) {
- h_word = word
- }
- }
-
- /* Let's invoke the init() function */
- hm.init("hello")
+ h_word = null
+
+ function init(word) {
+ h_word = word
+ }
+ }
+
+ /* Let's invoke the init() function */
+ hm.init("hello")
We're using `hm.init` to invoke the function which causes the value of `hm` to become the `this`
scope for this function call.
is the value of the last statement which was evaluated for the branch which was taken:
a = if (true) {
- log("Taking the 'true' branch")
- 7 * 3
- } else {
- log("Taking the 'false' branch")
- 9
- }
+ log("Taking the 'true' branch")
+ 7 * 3
+ } else {
+ log("Taking the 'false' branch")
+ 9
+ }
This example prints the log message "Taking the 'true' branch" and the `a` variable is set to 21 (7 * 3).
Example:
var list = [ "a", "b", "c" ]
-
- for (item in list) {
- log("Item: " + item)
- }
+
+ for (item in list) {
+ log("Item: " + item)
+ }
The loop body is evaluated once for each item in the array. The variable `item` is declared as a local
variable just as if the `var` keyword had been used.
Iterating over dictionaries can be accomplished in a similar manner:
var dict = { a = 3, b = 7 }
-
- for (key => value in dict) {
- log("Key: " + key + ", Value: " + value)
+
+ for (key => value in dict) {
+ log("Key: " + key + ", Value: " + value)
}
The `continue` and `break` keywords can be used to control how the loop is executed: The `continue` keyword
The type object's `prototype` property can be used to find out which methods a certain type
supports:
- /* This returns: ["find","len","lower","replace","split","substr","to_string","upper"] */
+ /* This returns: ["find","len","lower","replace","split","substr","to_string","upper"] */
keys(String.prototype)
## <a id="reserved-keywords"></a> Reserved Keywords