Saltar al contenido principal
Versión: Siguiente

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:

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
LanzamientoModificaciones
21 R3Soporte de objetos 4D.Methods
17 R3Añadidos

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

ParámetrosTipoDescripción
thisObjObject->Object to be returned by the This command in the function
paramsCollection->Collection of values to be passed as parameters to the function
Resultadoany<-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.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.

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
LanzamientoModificaciones
21 R3Soporte de objetos 4D.Methods
17 R3Añadidos

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

ParámetrosTipoDescripción
thisObjObject->Object to be returned by the This command in the function
paramsany->Values to be passed as parameters to the function
Resultadoany<-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.Formula objects, parameters are passed in $1...$n in the formula.
  • in 4D.Method objects, 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
LanzamientoModificaciones
21 R3Soporte de objetos 4D.Methods
18 R2Añ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)"