Skip to main content
Version: Next

Function

A 4D.Function object contains a piece of code that can be executed from an object, either using the () operator, or using the apply() and call() functions.

Inheritance

4D handles several kinds of Function objects, inheriting from the 4D.Function class:

Executing code in Function objects

Function objects can be encapsulated in object properties:

var $message : 4D.Formula
$message:=Formula(ALERT("Hello world"))
$f:={message: $message}

This property is an "object function", i.e. a function which is bound to its parent object. To execute a function stored in an object property, use the () operator after the property name, such as:

 $f.message() //displays "Hello world"

Syntax with brackets is also supported:

 $f["message"]() //displays "Hello world"

Note that, even if it does not have parameters (see below), an object function to be executed must be called with () parenthesis. Calling only the object property will return a new reference to the formula (and will not execute it):

 $o:=$f.message //returns the function object in $o

You can also execute a function using the apply() and call():

 $message.apply() //displays "Hello world"

Summary

.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any

executes the function object to which it is applied, passing parameters as a collection, and returns the resulting value
.call() : any
.call( thisObj : Object { ; ...params : any } ) : any

executes the function object to which it is applied, with one or more parameter(s) passed directly, and returns the resulting value
.source : Text
contains the source code of the function as text

.apply()

History
ReleaseChanges
21 R3Support of 4D.Methods objects
17 R3Added

.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any

ParameterTypeDescription
thisObjObject->Object to be returned by the This command in the function
paramsCollection->Collection of values to be passed as parameters to the function
Resultany<-Value from function execution

Description

The .apply() function executes the function object to which it is applied, passing parameters as a collection, and returns the resulting value.

In the thisObj parameter, you can pass a reference to the object to be used as This within the function. Pass Null if you do not want to use This but you want to send parameters.

You can pass a collection to be used as parameters in the function using the optional params parameter:

  • in 4D.Formula objects, parameters are passed in $1...$n in the formula.
  • in other 4D.Function objects such as 4D.Method objects, parameters are passed in declared method parameters.

Note that .apply() is similar to .call() except that parameters are passed as a collection. This can be useful for passing calculated results.

.call()

History
ReleaseChanges
21 R3Support of 4D.Methods objects
17 R3Added

.call() : any
.call( thisObj : Object { ; ...params : any } ) : any

ParameterTypeDescription
thisObjObject->Object to be returned by the This command in the function
paramsany->Values to be passed as parameters to the function
Resultany<-Value from function execution

Description

The .call() function executes the function object to which it is applied, with one or more parameter(s) passed directly, and returns the resulting value.

In the thisObj parameter, you can pass a reference to the object to be used as This within the function.

You can pass values to be used as parameters in the function using the optional params parameter:

  • in 4D.Formula objects, parameters are passed in $1...$n in the formula.
  • in 4D.Method objects, parameters are passed in declared method parameters.

Note that .call() is similar to .apply() except that parameters are passed directly.

Example 1

 var $f : 4D.Function
$f:=Formula(Uppercase($1))
$result:=$f.call(Null;"hello") // returns "HELLO"

Example 2

 $o:=New object("value";50)
$f:=Formula(This.value*2)
$result:=$f.call($o) // returns 100

.source

History
ReleaseChanges
21 R3Support of 4D.Methods objects
18 R2Added

.source : Text

Description

The .source property contains the source code of the function as text.

The returned value is the original text used to create the 4D.Formula or 4D.Method object but reformatted.

This property is read-only.

Example

 var $of : 4D.Function
var $tf : Text
$of:=Formula(String(Current time;HH MM AM PM))
$tf:=$of.source //"String(Current time;HH MM AM PM)"