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 Aktualizací přes RSS | E-mail Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Home / Kódování / JavaScript & AJAX / JavaScript: Poslat funkci ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Poslat funkci jako parametr jiné funkce (volání)

Posted on 29. Publikováno dne 29.. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Července 2009 od Dragos v Kódování, JavaScript & Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Určitě jste viděli mnoho kódu, kde funkce jsou odeslat jako parametry, obvykle pracuje jako volání funkce, např.:

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

But how does the function setTimeout execute the passed function as a parameter? Ale jak se funkce setTimeout vykonávat funkci předány jako parametr? The answer is simple, but I need to provide you an example to understand it Odpověď je jednoduchá, ale musím uvést jeden příklad se to pochopit ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Pojďme vytvořit jednoduchou funkci, přijímá dva parametry: první logickou hodnotu, druhý - funkce. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Naším úkolem bude analyzovat boolean parametr, a v případě, že hodnota je pravda, že funkce jako parametr bude popraven.

function simpleFunc(bool,func) { Funkce simpleFunc (bool, funkce) (
    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. Jak si všimnete, přidáme závorky za názvem druhý parametr, protože budeme brát to jako funkce. And that's te whole secret. A to je celé tajemství te.

Now that is how we will use the simpleFunc function: Nyní, když je, jak budeme používat simpleFunc funkce:

simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee!');) / / toto upozornění nebude nic, protože boolean parametr je falešný
//example two / / například dva
simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (pravda, function () (alert ( 'Yupee!');)); / / to bude varování Yupee! :)

Wish you luck! Přeji Vám hodně štěstí!

Translate this post Translate this post


Related posts: Související příspěvky:

  1. Javascript: How to validate email address with JavaScript? Javascript: Jak ověřit e-mailovou adresu JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Jak se dostat na indexu (pozici v rámci skupiny) z objektu s jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Co když jQuery animace není oheň / start?
  4. Coding: How to get code suggestions and function completion in Netbeans? Kódování: Jak získat kód návrhy a funkce dokončení v NetBeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Kódování: Jak načíst data profil uživatele SSI.php z databáze fórum SMF

    blog comments powered by Disqus blog připomínky powered by Disqus