We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

sendoa on 07/28/06


Tagged

argumentos opcionales funciones


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

Eloi


Argumentos opcionales en una función


Published in: JavaScript 


Cómo controlar los argumentos opcionales en funciones Javascript.

  1. var funcEjemplo = function(nombre){
  2. //Si el segundo argumento de la función contiene algo lo tenemos.
  3. //De lo contrario opciones es un objeto vacío.
  4. var opciones = arguments[1] || {};
  5.  
  6. //Asignamos las opciones a variables normales o ponemos valores por defecto
  7. var saludo = opciones.saludo || "Hola";
  8. var mensaje = opciones.mensaje || "¿Qué tal?";
  9.  
  10. //Hacemos algo supuestamente útil ;D
  11. alert(saludo + " " + nombre + ". " + mensaje);
  12. }
  13.  
  14. funcEjemplo("Sendoa", { saludo: "Agur", mensaje: "Ondo pasa!" } );

Report this snippet 

You need to login to post a comment.