next up previous contents
Next: Properties Up: FFish Script Objects Reference Previous: sup   Contents


Function Object

Description

Creates a new function.


Syntax

  1. function functionname([argname_1 [, ..., argname_N]])
    {
    body
    }
  2. var functionname = new Function( [argname_1, [...argname_N,]] body);


Parameters

functionname
The name of the newly created function
argname_1 ...argname_N
An optional list of arguments that the function accepts
body
A string that contains the block of Ffish Script code to be executed when the function is called.


Remarks

Syntax 1 is the standard way to create new functions in Ffish Script. Syntax 2 is an alternative form used to create function objects explicitly. Ffish script also support nested functions. A nested function object is a function placed in another function, it will become a member of the Object object constructed by the outer function. For example, we create an object to calculate the square roots:

function GetRoot(value)
{
    this.value = value;
    function root()
    {
        return Math.sqrt(this.value);
    }
}

gr = new GetRoot(4);
trace(gr.root());
//output: 2

an alternative version:

function GetRoot(value)
{
    function root(value)
    {
        return Math.sqrt(value);
    }
    return GetRoot.root(value);
}

trace(GetRoot(4));
//output: 2


Example

For example, to create a function that calculates summation of three numbers, you can do it in either of two ways:

//Example 1
function sum(x, y, z){  return x + y + z;}

//Example 2
var sum = new Function("x, y", "z",
    "return(x+y+z)");
//In either case,
//you call the function with a line of code similar
//to the following:
sum(1, 2, 3);




Subsections
next up previous contents
Next: Properties Up: FFish Script Objects Reference Previous: sup   Contents
Copyright ©2000-2010 Shanghai TopCMM Software Technologies. All Rights Reserved.