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 UPDATES VIA RSS | E-mail Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Home / Coding / JavaScript & Ajax / JavaScript: Stuur functie als ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Stuur functie als parameter aan een andere functie (callbacks)

Posted on 29. Geplaatst op 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Juli, 2009 door Dragos in Coding, JavaScript & Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Ik weet zeker dat je een hoop code waar functies zijn te verzenden als parameters, meestal werken als functie callbacks bijvoorbeeld gezien:

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

But how does the function setTimeout execute the passed function as a parameter? Maar hoe werkt de functie setTimeout de doorgegeven functie uitvoeren als parameter? The answer is simple, but I need to provide you an example to understand it Het antwoord is simpel, maar ik moet zorgen dat je een voorbeeld om het te begrijpen ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Laten we een eenvoudige functie, het aanvaarden van twee parameters: eerst een booleaanse waarde, de tweede - een functie. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Onze functie analyseert de boolean parameter en in het geval de waarde waar is, de functie doorgegeven als parameter zal worden uitgevoerd.

 function simpleFunc(bool,func) { functie 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. Zoals u merkt, voegen we haakjes achter de naam van de tweede parameter, omdat we het zullen beschouwen als een functie. And that's te whole secret. En dat is te hele geheim.

Now that is how we will use the simpleFunc function: Nu dat is hoe we de simpleFunc functie zal gebruiken:

simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false //example two simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (false, function () (alert ( 'Yupee!');) / / dit niet alert niets, omdat de boolean parameter false / / bijvoorbeeld twee simpleFunc (true, function () (( 'Yupee alert! ');)); / / dit zal alert Yupee! :)

Wish you luck! Wish you luck!

Translate this post Translate this post


Related posts: Gerelateerde berichten:

  1. Javascript: How to validate email address with JavaScript? Javascript: Hoe te valideren e-mailadres met JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Hoe krijg ik de index (positie binnen een groep) van een object met jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Wat als jQuery animatie niet brand / start?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Hoe krijg ik code suggesties en functie voltooiing in Netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Hoe te halen gebruikersprofiel gegevens met SSI.php uit een SMF forum database

    blog comments powered by Disqus blog comments powered by Disqus