Created
April 6, 2011 14:22
-
-
Save ggendre/905732 to your computer and use it in GitHub Desktop.
passer une fonction en paramètres, exemples 2/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 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