JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Pošljite funkcijo kot parameter na drugo delovno mesto (callbacks)
Posted on 29. Objavljeno dne 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Julij 2009, ki ga Dragos v Coding, JavaScript in Ajax
I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Prepričan sem, da ste videli veliko kode, če so funkcije poslati kot parametri, ki delajo kot običajno funkcija callbacks na primer:
setTimeout(function () { alert('test'); },1000); setTimeout (function () (alert ( 'test');), 1000);
But how does the function setTimeout execute the passed function as a parameter? Ampak kako deluje setTimeout izvršiti sprejel funkcijo kot parameter? The answer is simple, but I need to provide you an example to understand it Odgovor je preprost, vendar moram, da vam na primer, da razumem ![]()
Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Let's ustvariti preprosto funkcijo, sprejme dva parametra: prvi boolean vrednost, drugo - funkcije. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Naša funkcija bo analiziral boolean parameter, in v primeru, da vrednost je res, sprejel funkcijo kot parameter bo usmrčen.
function simpleFunc(bool,func) { Funkcija 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. Kot ste opazili, smo dodali oklepajih ime drugega parametra, saj bomo obravnavajo kot funkcija. And that's te whole secret. In to je skrivnost te celote.
Now that is how we will use the simpleFunc function: Zdaj, ko je, kako bomo uporabili simpleFunc funkcijo:
simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (false, function () (alert ( 'Yupee!');); / / to ne bo opozoril, ničesar, saj boolean parameter false //example two / / primer dva simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, function () (alert ( 'Yupee!');)); / / to bo opozorila Yupee!![]()
Wish you luck! Zaželi si srečo!
Related posts: Podobni objav:
- Javascript: How to validate email address with JavaScript? Javascript: kako potrditi e-naslov z JavaScript?
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Kako indeks (položaj v skupini) predmeta s jQuery?
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Kaj če jQuery animacije ne ognja / začeti?
- Coding: How to get code suggestions and function completion in Netbeans? Coding: Kako priti do kode predloge in delujejo v zaključku Netbeans?
- Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Kako puščati podatkov uporabnikov profil z SSI.php iz baze foruma SMF












































