JavaScript: Send function as a parameter to another function (callbacks) JavaScript: Send-Funktion als Parameter an eine andere Funktion (Rückrufe)
Posted on 29. Gesendet am 29. Jul, 2009 by Dragos in Coding , JavaScript & Ajax Juli 2009 von Dragos in Coding, JavaScript und Ajax
I'm sure you've seen a lot of code where functions are send as parameters, usually working as function callbacks eg: Ich bin sicher, Sie haben eine Menge Code, in dem Funktionen als Parameter zu senden, in der Regel arbeitet als Callback-Funktion zB gesehen:
setTimeout(function () { alert('test'); },1000); setTimeout (function () (alert ( 'test');), 1000);
But how does the function setTimeout execute the passed function as a parameter? Aber wie funktioniert die Funktion setTimeout führen Sie die Funktion übergeben als Parameter? The answer is simple, but I need to provide you an example to understand it Die Antwort ist einfach, aber ich muss Ihnen ein Beispiel zu verstehen ![]()
Let's create a simple function, accepting two parameters: first a boolean value, the second – a function. Lassen Sie uns eine einfache Funktion, akzeptiert zwei Parameter: zuerst ein Boolescher Wert, der zweite - eine Funktion. Our function will analyze the boolean parameter and in case the value is true , the function passed as a parameter will be executed. Unsere Aufgabe wird die boolean-Parameter zu analysieren und im Falle der Wert true ist, wird die Funktion als Parameter übergeben wird ausgeführt.
function simpleFunc(bool,func) { Funktion 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. Wie Sie sehen, fügen wir Klammern nach dem Namen des zweiten Parameters, weil wir es dann als eine Funktion zu behandeln. And that's te whole secret. Und das ganze Geheimnis te.
Now that is how we will use the simpleFunc function: Nun ist, wie wir die simpleFunc Funktion zu nutzen wird:
simpleFunc(false,function() { alert('Yupee!'); }; // this won't alert anything, because the boolean parameter is false //example two simpleFunc(true,function() { alert('Yupee!'); }); // this will alert Yupee! simpleFunc (false, function () (alert ( 'Yupee! ");) / / das wird nicht Ausschreibung nichts, weil der boolesche Parameter ist false / / Beispiel zwei simpleFunc (true, function () ((' Yupee alert! ');)); / / this will alert Yupee!
Wish you luck! Wünsche Ihnen viel Glück!
Related posts: In Verbindung stehende Pfosten:
- Javascript: How to validate email address with JavaScript? Javascript: Wie für die Validierung der E-Mail-Adresse mit JavaScript?
- JavaScript: How to get the index (position within a group) of an object with jQuery? JavaScript: Wie in den Index zu erhalten (Position innerhalb einer Gruppe) eines Objekts mit jQuery?
- JavaScript: What if jQuery animation doesn't fire/start? JavaScript: Was passiert, wenn jQuery Animation nicht Feuer / start?
- Coding: How to get code suggestions and function completion in Netbeans? Coding: Der Weg zum Kodex-Anregungen und Funktion Abschluss in Netbeans?
- Coding:How to fetch user profile data with SSI.php from a SMF forum database Coding: Wie zum Benutzerprofil Daten mit SSI.php holen aus einer SMF-Forum Datenbank












































