Function
Un objeto 4D.Function contiene un trozo de código que puede ser ejecutado desde un objeto, ya sea utilizando el operador (), o utilizando las funciones apply() y call().
Herencia
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}
Esta propiedad es una "función objeto", es decir una función que está vinculada a su objeto padre. Para ejecutar una función almacenada en una propiedad objeto, utilice el operador () después del nombre de la propiedad, como:
$f.message() //muestra "Hello world"
También se admite la sintaxis con paréntesis:
$f["message"]() //muestra "Hello world"
Note that, even if it does not have parameters (see below), an object function to be executed must be called with () parenthesis. Llamar sólo a la propiedad del objeto devolverá una nueva referencia a la fórmula (y no la ejecutará):
$o:=$f.message //returns the function object in $o
You can also execute a function using the apply() and call():
$message.apply() //muestra "Hello world"
Resumen
| .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()
Historia
| Lanzamiento | Modificaciones |
|---|---|
| 21 R3 | Soporte de objetos 4D.Methods |
| 17 R3 | Añadidos |
.apply() : any
.apply( thisObj : Object { ; params : Collection } ) : any
| Parámetros | Tipo | Descripción | |
|---|---|---|---|
| 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 |
| Resultado | any | <- | Valor de la ejecución de la función |
Descripción
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. Pasa Null si no quiere utilizar This pero quiere enviar parámetros.
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.
Tenga en cuenta que .apply() es similar a .call() excepto que los parámetros se pasan como una colección. Esto puede ser útil para pasar los resultados calculados.
.call()
Historia
| Lanzamiento | Modificaciones |
|---|---|
| 21 R3 | Soporte de objetos 4D.Methods |
| 17 R3 | Añadidos |
.call() : any
.call( thisObj : Object { ; ...params : any } ) : any
| Parámetros | Tipo | Descripción | |
|---|---|---|---|
| thisObj | Object | -> | Object to be returned by the This command in the function |
| params | any | -> | Values to be passed as parameters to the function |
| Resultado | any | <- | Valor de la ejecución de la función |
Descripción
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.
Tenga en cuenta que .call() es similar a .apply() excepto que los parámetros se pasan directamente.
Ejemplo 1
var $f : 4D.Function
$f:=Formula(Uppercase($1))
$result:=$f.call(Null;"hello") // devuelve "HELLO"
Ejemplo 2
$o:=New object("value";50)
$f:=Formula(This.value*2)
$result:=$f.call($o) // devuelve 100
.source
Historia
| Lanzamiento | Modificaciones |
|---|---|
| 21 R3 | Soporte de objetos 4D.Methods |
| 18 R2 | Añadidos |
.source : Text
Descripción
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.
Esta propiedad es de solo lectura.
Ejemplo
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)"