JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Enviar funcionar com un paràmetre a una altra funció (callbacks)
Posted on 29. Publicat el 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Juliol de 2009, per Dragos a la codificació, JavaScript i Ajax
I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Estic segur que has vist un munt de codi, on les funcions són enviar com a paràmetres, generalment treballen com a devolucions de trucada de funció, per exemple:
setTimeout(function () { alert('test'); },1000); setTimeout (function () (alert ( 'test');), 1000);
But how does the function setTimeout execute the passed function as a parameter? Però, com la funció setTimeout d'executar la funció es passa com a paràmetre? The answer is simple, but I need to provide you an example to understand it La resposta és senzilla, però he de donar-te un exemple per entendre-ho ![]()
Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Anem a crear una funció senzilla, acceptar dos paràmetres: en primer lloc un valor booleà, el segon - una funció. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. La nostra funció és analitzar el paràmetre booleà i, en cas que el valor és true, la funció passada com a paràmetre serà executat.
function simpleFunc(bool,func) { funció 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. Com s'observa, s'hi afegeix entre parèntesis després del nom del segon paràmetre, perquè anem a tractar-lo com una funció. And that's te whole secret. I això és secret et.
Now that is how we will use the simpleFunc function: Ara que és com anem a utilitzar la funció de simpleFunc:
simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (fals, function () (alert ( 'Yupee !');) / / això no alertar res, perquè el paràmetre booleà és fals //example two / / Exemple dos simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee !');)); / / Això alertarà Yupee!![]()
Wish you luck! Li desitjo sort!
Related posts: Llocs relacionats amb:
- Javascript: How to validate email address with JavaScript? Javascript: Com validar l'adreça de correu electrònic amb JavaScript?
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Com obtenir l'índex (posició dins d'un grup) d'un objecte amb jQuery?
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Què passa si l'animació jQuery no es dispara / inici?
- Coding: How to get code suggestions and function completion in Netbeans? Codificació: Com arribar suggeriments sobre el codi i completar la funció en Netbeans?
- Coding:How to fetch user profile data with SSI.php from a SMF forum database Codificació: Com buscar dades de perfils d'usuari amb SSI.php d'una base de dades de fòrum SMF












































