Javascript closures function parameters?

后端 未结 3 1570
不思量自难忘°
不思量自难忘° 2021-02-09 02:47

Code belongs to javascriptissexy.com My question is why invoking mjName (\"Jackson\") returns \"This celebrity is Michael Jackson\"? Is it that second parameter given in ANY out

3条回答
  •  情歌与酒
    2021-02-09 03:37

    With this call

    var mjName = celebrityName ("Michael");
    

    you create a custom function, which has the firstName variable bound to "Michael". This function is returned to you by celebrityName().

    When you call that returned function again, you bind lastName as well, which results in your output.

    If you want to bind another first name, you have to call celebrityName() again.

    var michaelName = celebrityName( "Michael" );
    var davidName =   celebrityName( "David" );
    
    michaelName( "Jackson" ); // yields "This celebrity is Michael Jackson"
    davidName( "Duchovny" );  // yields "This celebrity is David Duchovny"
    

提交回复
热议问题