Foros del Web » Creando para Internet » Flash y Actionscript »

funciones con parámetros opcionales

Estas en el tema de funciones con parámetros opcionales en el foro de Flash y Actionscript en Foros del Web. es posible crear en flash, funciones con parámetros opcionales? y si es asi podrían darme un ejemplo? gracias...
  #1 (permalink)  
Antiguo 10/12/2004, 16:32
Avatar de dexter_x740  
Fecha de Ingreso: julio-2003
Ubicación: cd. obregon, Sonora
Mensajes: 246
Antigüedad: 20 años, 9 meses
Puntos: 1
funciones con parámetros opcionales

es posible crear en flash, funciones con parámetros opcionales? y si es asi podrían darme un ejemplo?
gracias
  #2 (permalink)  
Antiguo 10/12/2004, 16:35
Avatar de living  
Fecha de Ingreso: mayo-2004
Mensajes: 1.266
Antigüedad: 19 años, 11 meses
Puntos: 2
Que yo sepa actionscript no permite sobrecarga de métodos.
__________________
¿Te apasiona el mundo del guión? El portal del guión
  #3 (permalink)  
Antiguo 10/12/2004, 17:45
Avatar de Prince  
Fecha de Ingreso: mayo-2003
Ubicación: DF
Mensajes: 574
Antigüedad: 20 años, 11 meses
Puntos: 1
Bueno, de hecho sí se puede. Es como un truco.
Creas una función, si lo deseas, que no reciba ningún parámetro...
Código:
function alguna(){
  trace("Función que no recibe parámetros");
}
... y después...
te voy a ser sincero, no tengo ahorita a la mano un ejemplo de eso, pero tienes que hacer uso de las funciones "caller" y "calee" de action Script. Lee un poco de estas funciones en la ayuda para que te des una idea.
__________________
- P R I N C E -
  #4 (permalink)  
Antiguo 10/12/2004, 20:14
Avatar de Prince  
Fecha de Ingreso: mayo-2003
Ubicación: DF
Mensajes: 574
Antigüedad: 20 años, 11 meses
Puntos: 1
Ok ok, tuve un error, no se necesitaban "caller" ni "calee" pero ya tengo el ejemplo de como hacerlo.

Posiblemente este sea un buen ejemplo para dejarlo en las FAQ de este foro.

- OVERLOADING A FUNCTION -
Overloading a function normally involves having multiple functions with the same name but with different numbers of parameters. This can be useful in a lot of situations. For example, you could have a function named calculateArea() that calculates the area of a rectangle based on two parameters (the lengths of the sides). But you might also want to have a calculateArea() function that calculates the area on a circle based on a single parameter (the radius). The trouble is that, as we know, ActionScript does not require symmetry between the number of parameters defined for a function and the number of parameters passed to it. That means that you cannot have two functions with the same name -even if they expect different numbers of parameters-.
You can simulate function overloading if you desire by using if statements in the function to check for the number of parameters. The following shows how you can write a function that calculates the area of either a rectangle or a circle depending on the number of parameters it is passed (determined by 'arguments.length'):
Código:
function calculateArea():Number{
  switch(arguments.length){
    case 1:
      var radius:Number = arguments[0];
      return (Math.PI * (radius * radius));
    case 2:
      var a:Number = arguments[0];
      var b:Number = arguments[1];
      return (a * b);
    default:
      return null;
  }
}
Bueno, como podemos ver todo lo controlarás con el array 'arguments', en cual controla, dentro de la función, el número de argumentos que le hemos pasado a esta.

SALUDOS
__________________
- P R I N C E -
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:33.