-
Notifications
You must be signed in to change notification settings - Fork 35
Functions
Function is a one of object type that could be called by '(' and ')' paired operation.
Function could be defined using following syntax:
function identifier(argument-list) {
statements
}
where argument-list is
variable[, variable]*
See more about Statements.
For example:
function hello() {
console.log('hello world!');
}
Syntax to call a defined function:
hello()
For passing arguments into function, the syntax of definition is:
function show(a, b) {
console.log(a + b);
}
Then call this function by passing arguments:
show(10, 5);
The result is:
15
The function override is not supported by ReoScript. However, you could simulate a same effect by using argument list. See Variable Number of Arguments below.
ReoScript finds and calls a function by specified function name, even given arguments is not matched to that function. For example:
function plus(a, b, c) {
return a + b + c;
}
Call this function:
var z = plus(1, 2, 3); // 'z' is 6
This function can also be called with different number of arguments:
var z = plus(1, 2); // calling is OK, but 'z' is NaN since 'c' is null
Arguments that not specified in calling statement will be set to null automatically. The 'a + b + c' will returns NaN because 'c' is null(+ operation does not work with numbers and null). To solve this issue, you could:
- Check arguments that be specified or not
- Use 'args' system variable
For example:
function plus(a, b, c) {
return c == null ? (a + b) : (a + b + c);
}
Or you could use an argument array called 'args', it is an array contains all of variables what passed by calling statements:
function plus(a, b, c) {
return __args__.length == 3 ? (a + b + c) : (a + b);
}