Chapter 3 DynaScript Predefined Objects


Function object

Object

Enables the creation of functions.

Standard: ECMAScript

Syntax

To use a Function property:

Function.propertyName

To use a Function method:

Function.MethodName( parameter ) 

Description

The function constructor can be used to create a function on the fly:

funcName = new Function( [[parameter1 [, parameter2...]], body] ); 

The function keyword can also be used to create a function, in which case the function is not created until it has been called from somewhere else in the script:

function funcName ( [parameter1 [, parameter2...]], ) {

    body

} 

Example

This example creates two Function objects called square and square1 and executes them:

<!--SCRIPT 
    // function created with a constructor
    square = new Function( "number", "return number * number;" );
    
    // function created with the keyword
    function square1 ( number) {
        return number * number
    }
 
    document.writeln( square(5) );
    document.writeln( square1(5) );
-->

length property

Syntax

Function.length 

Attributes

This property is read-only.

Description

Returns the number of parameters required by the Function.

Return

Integer

Example

This example returns the number of parameters required by the function:

<!--SCRIPT
    myFunc = new Function( "x","y", "return x * y;" );
    document.WriteLn( "The function myFunc takes " + myFunc.length + " parameters." );
    document.WriteLn( myFunc(5, 6) );
-->

toString method

Syntax

Function.toString( )

Description

Returns the body of the function as a string.

Return

String

Example

This example displays the body of the function:

<!--SCRIPT 
    myFunc = new Function( "x","y", "return x * y;" );
    document.writeln( myFunc(5, 6) );
    document.WriteLn( myFunc.toString() );
-->

 


Copyright © 2001 Sybase, Inc. All rights reserved.