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 UPDATES VIA RSS | Email Get updates via feedburner Get updates via twitter
Home / Coding / JavaScript & Ajax / JavaScript: Send function as… Home / Coding / JavaScript & Ajax / JavaScript: Kirim berfungsi sebagai ...

JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Kirim fungsi sebagai parameter ke fungsi lain (callback)

Posted on 29. Dikirim di 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Jul, 2009 oleh Dragos di Coding, JavaScript & Ajax

I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Aku yakin kau sudah melihat banyak kode mana fungsi mengirim sebagai parameter, biasanya bekerja sebagai fungsi callback misalnya:

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

But how does the function setTimeout execute the passed function as a parameter? Tapi bagaimana fungsi setTimeout mengeksekusi berlalu fungsi sebagai parameter? The answer is simple, but I need to provide you an example to understand it Jawabannya sederhana, tapi aku perlu memberikan Anda contoh untuk memahaminya ;)

Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Mari's menciptakan fungsi sederhana, menerima dua parameter: pertama sebuah boolean nilai, kedua - sebuah fungsi. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Fungsi kita akan menganalisis boolean parameter dan dalam kasus nilai benar, fungsi berlalu sebagai parameter akan dieksekusi.

 function simpleFunc(bool,func) { fungsi simpleFunc (bool, func) ( 
     if(bool) func(); jika (bool) func (); 
 } ) 

As you notice, we add parentheses after the name of the second parameter, because we'll treat it as a function. Sebagai Anda perhatikan, kita menambahkan kurung setelah nama parameter kedua, karena kita akan memperlakukannya sebagai fungsi. And that's te whole secret. Dan bahwa's te seluruh rahasia.

Now that is how we will use the simpleFunc function: Sekarang bahwa adalah bagaimana kita akan menggunakan simpleFunc fungsi:

 simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false simpleFunc (palsu, fungsi () (alert ( 'yupee!');); / / ini tidak akan waspada apa pun, karena boolean parameter adalah palsu 
 //example two / Contoh dua 
 simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (true, fungsi () (alert ( 'yupee!');)); / / ini akan alert yupee! )

Wish you luck! Wish Anda beruntung!

Translate this post Terjemahkan posting ini


Related posts: Related posts:

  1. Javascript: How to validate email address with JavaScript? Javascript: Bagaimana untuk memvalidasi alamat email dengan JavaScript?
  2. JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Cara mendapatkan indeks (posisi dalam grup) dari sebuah objek dengan jQuery?
  3. JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Bagaimana jika jQuery animasi tidak api / mulai?
  4. Coding: How to get code suggestions and function completion in Netbeans? Coding: Cara dapatkan kode saran dan fungsi penyelesaian di Netbeans?
  5. Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Bagaimana untuk mengambil data dengan profil pengguna SSI.php dari database forum SMF

    blog comments powered by Disqus blog comments powered by DISQUS