Function
Um objeto 4D.Function contém um trecho de código que pode ser executado a partir de um objeto, usando o operador (), ou usando as funções apply() e call().
Herança
4D handles several kinds of Function objects, inheriting from the 4D.Function class:
- native functions, i.e. built-in functions from various 4D classes such as
collection.sort()orfile.copyTo(). - user functions, created in user classes using the
Functionkeyword. - formula functions, i.e. functions that can execute formula code stored in 4D.Formula objects,
- method functions, i.e. functions that can execute source code as text stored in 4D.Method objects.
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}
Essa propriedade é uma "função objeto" ou seja uma função que é restrita a seu objeto pai. Para executar uma função armazenada em uma propriedade objeto, use o operador () depois do nome propriedade, tal como:
$f.message() //exibe "Hello world"
Também se admite a sintaxe com parênteses:
$f["message"]() //exibe "Hello world"
Note that, even if it does not have parameters (see below), an object function to be executed must be called with () parenthesis. Chamar só a propriedade de objeto devolverá uma nova referência à fórmula (e não a executará):
$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"
Resumo
| .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()
História
| Release | Mudanças |
|---|---|
| 21 R3 | Support of 4D.Methods objects |
| 17 R3 | Adicionado |
.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any
| Parâmetro | Tipo | Descrição | |
|---|---|---|---|
| thisObj | Object | -> | Object to be returned by the This command in the function |
| params | Collection | -> | Collection of values to be passed as parameters to the function |
| Resultados | any | <- | Value from function execution |
Descrição
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.Formulaobjects, parameters are passed in $1...$n in the formula. - in other
4D.Functionobjects such as4D.Methodobjects, parameters are passed in declared method parameters.
Note que .apply() é similar a .call() exceto que os parâmetros são passados como coleção. Isso pode ser útil para passar resultados calculados. Isso pode ser útil para passar resultados calculados.
.call()
História
| Release | Mudanças |
|---|---|
| 21 R3 | Support of 4D.Methods objects |
| 17 R3 | Adicionado |
.call() : any
.call( thisObj : Object { ; ...params : any } ) : any
| Parâmetro | Tipo | Descrição | |
|---|---|---|---|
| thisObj | Object | -> | Object to be returned by the This command in the function |
| params | any | -> | Values to be passed as parameters to the function |
| Resultados | any | <- | Value from function execution |
Descrição
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.Formulaobjects, parameters are passed in $1...$n in the formula. - in
4D.Methodobjects, parameters are passed in declared method parameters.
Observe que .call() é semelhante a .apply(), exceto pelo fato de que os parâmetros são passados diretamente.
Exemplo 1
var $f : 4D. Function
$f:=Formula(Uppercase($1))
$result:=$f.call(Null;"hello") // retorna "HELLO"
Exemplo 2
$o:=New object("value";50)
$f:=Formula(This.value*2)
$result:=$f.call($o) // devolve 100
.source
História
| Release | Mudanças |
|---|---|
| 21 R3 | Support of 4D.Methods objects |
| 18 R2 | Adicionado |
.source : Text
Descrição
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.
Essa propriedade é somente leitura.
Exemplo
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)"