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 Uppdateringar via RSS | E-post Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Hem / Kodning / JavaScript / Ajax / JavaScript: Skicka fungera som ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Skicka fungera som en parameter till en annan funktion (callbacks)

Posted on 29. Skrivet den 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Juli, 2009 av Dragos i Coding, JavaScript och Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Jag är säker på att du har sett mycket av koden där funktioner skicka som parametrar, vanligen arbetar som funktion callbacks t ex:

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

But how does the function setTimeout execute the passed function as a parameter? Men hur fungerar setTimeout verkställa passerade fungera som en parameter? The answer is simple, but I need to provide you an example to understand it Svaret är enkelt, men jag måste ge er ett exempel för att förstå det ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Låt oss skapa en enkel funktion, emot två parametrar: först ett logiskt värde, den andra - en funktion. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Vår funktion analyserar boolean parameter och ifall värdet är sant, funktionen överförs som en parameter kommer att verkställas.

 function simpleFunc(bool,func) { funktion 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. Som ni märker, lägger vi parentes efter namnet på den andra parametern, eftersom vi kommer att behandla det som en funktion. And that's te whole secret. Och det är te hela hemligheten.

Now that is how we will use the simpleFunc function: Nu är hur vi kommer att använda simpleFunc funktion:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (falskt, function () (alert ( 'Yupee!');); / / detta inte kommer att varna någonting, eftersom boolean parameter är falsk 
 //example two / / exempel två 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (sant, function () (alert ( 'Yupee!');)); / / här kommer alert Yupee! :)

Wish you luck! Önskar er lycka till!

Translate this post Översätta det här inlägget


Related posts: Relaterade tjänster:

  1. Javascript: How to validate email address with JavaScript? Javascript: Hur validera e-postadress med JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Hur man får indexet (position inom en grupp) för ett objekt med jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Vad händer om jQuery animation inte eld / start?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Hur man får kod förslag och funktion avslutas i NetBeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Hur hämta data användarprofil med SSI.php från SMF forum databas

    blog comments powered by Disqus blogg kommentarer drivs av Disqus