-
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 can be defined using following syntax:
function identifier(argument-list) {
statements
}
where argument-list is
identifier[, identifier]*
See full Language Specification.
For example:
function hello() {
console.log('hello world!');
}
Syntax to call a defined function:
hello()
To pass arguments into function, the syntax of definition is:
function show(a, b) {
console.log(a + b);
}
Then call this function with arguments:
show(10, 5);
The result is:
15
The function override is not supported by ReoScript. However, you could simulate the same effect by using argument array. See Variable Number of Arguments below.
ReoScript finds and calls a function by specified function name, even given arguments does 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 __args__
, it is an array of all of variables:
function plus(a, b, c) {
return __args__.length == 3 ? (a + b + c) : (a + b);
}
Another example that joins all of arguments into one string using __args__
.
function join() {
var output = '';
for (str in __args__) {
if (output.length > 0) output += '-';
output += str;
}
return output;
}
Use this function:
var result = join('The', 'Earth', 'and', 'Sun');
The result is:
The-Earth-and-Sun
Since function is also an object stored as variable or property, the ruler to call functions is same as Variables. All functions defined in outer scope can be called in inner scope. Functions can also defined in local call-scope. For example:
function outer() {
}
outer
is global object(defined as property to global object) is available to all of call-scopes:
function fun_b() {
outer(); // OK
}
It is possible to define a function inside another function, for example:
function outer() {
function inner() {
}
}
Actually it is same as:
function outer() {
var inner = function() {
};
}
Inner function can only be called inside inner scope:
function outer() {
var inner = function() {
};
inner(); // OK
}
inner(); // error: Function is not defined: 'inner'
Notice that inner
is not a property of outer
:
outer.inner(); // error
Since outer.inner
is null so you will get an error function outer() {...} has no method 'inner'
.
There is no way to call an inner function from outside scope, the inner function is only available to the scope itself.
You could call a function in itself to implement recursive calling:
function findMore(path) {
if (path.hasMore()) {
return find_more(path.getMore());
}
}
A function defined without a specified name is an anonymous function. For example:
var hello = function() { };
Anonymous function must be defined, assigned to a variable or property, or called immediately.
var hello = function() { }; // OK
function() { }; // error: not is a valid expression-statement, it needs left-hand
function() { } (); // OK: call an anonymous function immediately