Skip to content

Instantly share code, notes, and snippets.

@ggendre
Created April 6, 2011 14:11
Show Gist options
  • Save ggendre/905718 to your computer and use it in GitHub Desktop.
Save ggendre/905718 to your computer and use it in GitHub Desktop.
passer une fonction en paramètre, exemples 1/2
//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