Created
April 6, 2011 14:11
-
-
Save ggendre/905718 to your computer and use it in GitHub Desktop.
passer une fonction en paramètre, exemples 1/2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//passer une fonction en paramètre, exemples 1/2 | |
function funct_1(){ | |
alert('toto') | |
} | |
function directCall(f) | |
{ | |
if (!f){ | |
alert('f not set'); | |
}else{ | |
f(); | |
} | |
} | |
directCall() // affichera 'f not set' | |
directCall(funct_1) // affichera 'toto' | |
directCall(function(){alert('tata')}) // affichera 'tata' | |
directCall('lkjlkj') // n'affichera rien | |
directCall('funct_1') // n'affichera rien | |
directCall('alert("toto")') // n'affichera rien | |
//note: il est possible de tester l'existence de la fonction | |
//avec if (f!=undefined and f.typeof=='function') si on veut être précis | |
//dans ce cas les 3 derniers exemples afficheront "f not set" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment