JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Wyślij funkcji jako parametr do innej funkcji (callback)
Posted on 29. Zamieszczone w dniu 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Lip, 2009 Dragos w Coding, 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: Jestem pewien, że widziałem wiele kodu, gdzie funkcje wysłać jako parametry, zwykle pracuje jako callback funkcji np.:
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 setTimeout wykonać przekazywane funkcji jako parametr? The answer is simple, but I need to provide you an example to understand it Odpowiedź jest prosta, ale muszę podać jakiś przykład to zrozumieć ![]()
Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Stwórzmy prostą funkcję, przyjmując dwa parametry: pierwszy wartość logiczną, drugi - funkcję. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Nasza funkcja będzie analizować logiczną parametrem w przypadku gdy wartość jest prawdą, funkcję przekazaną jako parametr zostanie wykonane.
function simpleFunc(bool,func) { Funkcja 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. Jak można zauważyć, dodamy nawiasach po nazwie drugi parametr, ponieważ będziemy traktować jako funkcję. And that's te whole secret. I właśnie te cała tajemnica.
Now that is how we will use the simpleFunc function: Teraz jest sposobie wykorzystania simpleFunc funkcji:
simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee! ");) / / to nie będzie informować o nic, bo boolean parametr jest fałszywe //example two / / przykład dwie simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee');)); / / to będzie ostrzegać Yupee!![]()
Wish you luck! Wish you luck!
Related posts: Podobne posty:
- Javascript: How to validate email address with JavaScript? Javascript: Jak potwierdzić adres e-mail z JavaScript?
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Jak zdobyć indeks (pozycja w grupie) obiektu z jQuery?
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Co zrobić, jeśli jQuery animacji nie ognia / start?
- Coding: How to get code suggestions and function completion in Netbeans? Kodowanie: Jak uzyskać kod sugestie i zakończenie funkcji w Netbeans?
- Coding:How to fetch user profile data with SSI.php from a SMF forum database Kodowanie: Jak pobrać dane z profilu użytkownika SSI.php z bazy danych SMF forum












































