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 FRISSÍTÉSEK VIA RSS | E-mail Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Home / Kódolás / JavaScript és Ajax / JavaScript: Send működni ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send függvény paraméterként egy másik funkció (visszahívások)

Posted on 29. Posted on 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Július, 2009 Dragos ben Kódolás, JavaScript és Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Biztos vagyok benne, láttam egy csomó kódot, ahol funkciók küld paraméterként, általában dolgozó funkció visszahívások például:

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

But how does the function setTimeout execute the passed function as a parameter? De hogyan működik a függvény setTimeout végre az elmúlt működnek a paraméter? The answer is simple, but I need to provide you an example to understand it A válasz egyszerű, de kell, hogy mondjak egy példát, hogy megértsék azt ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Hozzunk létre egy egyszerű funkció, elfogadva két paramétert: először egy logikai érték, a második - a funkció. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Dolgunk elemzik a boolean típusú paramétert, és amennyiben az érték igaz, akkor a függvény paraméterként átadásra kerül végrehajtásra.

 function simpleFunc(bool,func) { function 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. Mint azt veszi észre, mi adjuk a neve után zárójelben a második paraméter, mert majd kezelje azt a funkciót. And that's te whole secret. És ez az, te egész titok.

Now that is how we will use the simpleFunc function: Most, hogy ez hogyan fogja használni a simpleFunc funkció:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee!');) / / ez nem éber semmit, mert a paraméter logikai hamis 
 //example two / / például két 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (igaz, function () (alert ( 'Yupee!');)); / / ez figyelmezteti Yupee! :)

Wish you luck! Wish you luck!

Translate this post Translate this post


Related posts: Related posts:

  1. Javascript: How to validate email address with JavaScript? Javascript: How to validate e-mail címét a JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Hogyan juthat az index (position csoportján belül) egy objektum jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: mi van, ha jQuery animáció nem villan / start?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Hogyan szerezhető be a kódot javaslatokat és a funkció befejezése a Netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Hogyan lehet letölteni a felhasználói profil adatok SSI.php egy SMF fórum adatbázis

    blog comments powered by Disqus iPhone Disqus szerint