This is a translated page. The original can be found here: http://iwebdevel.com/2009/07/29/javascript-send-function-as-a-parameter-to-another-function-callbacks/
UPDATES VIA RSS | Email ACTUALIZACIONES VIA RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Inicio / codificación / JavaScript y Ajax / JavaScript: Enviar funcionar como ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Enviar funcionar como un parámetro a otra función (callbacks)

Posted on 29. Publicado el 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Julio de 2009, por Dragos en la codificación, JavaScript y Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Estoy seguro de que has visto un montón de código, donde las funciones son enviar como parámetros, por lo general trabajan como devoluciones de llamada de función, por ejemplo:

 setTimeout(function () { alert('test'); },1000); setTimeout (function () (alert ( 'test');), 1000); 

But how does the function setTimeout execute the passed function as a parameter? Pero, ¿cómo la función setTimeout de ejecutar la función se pasa como parámetro? The answer is simple, but I need to provide you an example to understand it La respuesta es sencilla, pero tengo que darte un ejemplo para entenderlo ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Vamos a crear una función sencilla, aceptar dos parámetros: en primer lugar un valor booleano, el segundo - una función. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Nuestra función será analizar el parámetro booleano y, en caso de que el valor es true, la función pasada como parámetro será ejecutado.

 function simpleFunc(bool,func) { función simpleFunc (bool, func) ( 
     if(bool) func(); if (bool) func (); 
 } ) 

As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. Como se observa, se añade entre paréntesis después del nombre del segundo parámetro, porque vamos a tratarlo como una función. And that's te whole secret. Y eso es secreto te.

Now that is how we will use the simpleFunc function: Ahora que es cómo vamos a utilizar la función de simpleFunc:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (falso, function () (alert ( 'Yupee!');) / / esto no alertar a nada, porque el parámetro booleano es falso 
 //example two / / ejemplo dos 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee!');)); / / Esto alertará Yupee! :)

Wish you luck! Le deseo suerte!

Translate this post Traducir este mensaje


Related posts: Puestos relacionados con:

  1. Javascript: How to validate email address with JavaScript? Javascript: Cómo validar la dirección de correo electrónico con JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Cómo obtener el índice (posición dentro de un grupo) de un objeto con jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: ¿Qué pasa si la animación jQuery no se dispara / inicio?
  4. Coding: How to get code suggestions and function completion in Netbeans? Codificación: Cómo llegar sugerencias sobre el código y completar la función en Netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Codificación: Cómo buscar datos de perfiles de usuario con SSI.php de una base de datos de foro SMF

    blog comments powered by Disqus blog alimentado por Disqus