irScript functions can be declared in one of two ways, with a name or without one. The latter are known as anonymous functions, and are conceptually (and behaviorally) similar to anonymous functions in languages like C#.
As a dynamic, implicitly-typed language irScript function arguments are not declared with a static type. Rather, values of any type are permitted for any function argument; the usage of the argument is of course a limiting factor on the ultimate set of usable types for any situation.
Here’s an example of named function declaration:
function someFunc ( arg1, arg2 ) { return arg1 / arg2; }
var result = someFunc( 88, 90 );
Upon declaration, a named function’s name is added to the enclosing variable scope (global or local, depending on circumstance). Afterwards, the function can be referred to by name for invocation, use as an argument to another function, etc. Note that if a variable or other function already exists with the same name, an error is thrown at the time of declaration.
Here are some examples of anonymous function declaration and usage:
var x = function ( arg ) { return arg * arg; };
var y = x( 98.8 );
var reverseCollectionManipulator = function ( collection, func )
{
return collection.Reverse.ForEach( func );
};
var myCollection = [1,77,88.09];
var newCollection = reverseCollectionManipulator( myCollection, function ( item )
{
return item * 2;
} );
Comments
0 comments
Please sign in to leave a comment.