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 Posodobitev prek RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Domov / Coding / JavaScript in Ajax / JavaScript: Pošljite funkcijo ...

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!

Translate this post Prevedi to post


Related posts: Podobni objav:

  1. Javascript: How to validate email address with JavaScript? Javascript: kako potrditi e-naslov z JavaScript?
  2. 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?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Kaj če jQuery animacije ne ognja / začeti?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Kako priti do kode predloge in delujejo v zaključku Netbeans?
  5. 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

    blog comments powered by Disqus blog comments powered by Disqus