Skip to content

Instantly share code, notes, and snippets.

@ggendre
Created April 6, 2011 14:22
Show Gist options
  • Save ggendre/905732 to your computer and use it in GitHub Desktop.
Save ggendre/905732 to your computer and use it in GitHub Desktop.
passer une fonction en paramètres, exemples 2/2
//passer une fonction en parametre, exemples 2/2
function doubler(x)
{
if (!x) alert('x not set');
else alert(x*2);
}
function callParam(f,a)
{
f(a);
}
callParam(doubler,5)
//-->affichera 10
callParam(doubler)
//-->affichera "x not set"
callParam(function(a){alert(a/2)},10)
//-->affichera 5
callParam(function(a){
if (!a)
alert('a not set');
else
alert(a/2)
},10)
//-->affichera 5
callParam(function(a){
if (!a)
alert('a not set');
else
alert(a/2)
})
//--> affichera "a not set"
callParam(function(){
if (!a)
alert('a not set');
else
alert(a/2)
},10)
//-->n'affichera rien car paramètre absent au niveau de function(a)
//callParam ne trouve pas f(a), vu que seul le prototype f() existe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment