Javascript闭包函数参数? [英] Javascript closures function parameters?

查看:90
本文介绍了Javascript闭包函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码属于javascriptissexy.com
我的问题是为什么调用mjName(Jackson)返回这个名人是迈克尔·杰克逊?
是否为ANY外部函数中给出的第二个参数,说到js = inner函数参数?
有人可以非常详细地解释整个概念吗?

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 outer function always, says to js = inner function parameter? Could someone explain the whole concept in great detail?

function celebrityName (firstName) {
    var nameIntro = "This celebrity is ";
// this inner function has access to the outer function's variables, including the parameter
    function lastName (theLastName) {
        return nameIntro + firstName + " " + theLastName;
    }
    return lastName;
}

var mjName = celebrityName ("Michael");
    // At this juncture, the celebrityName outer function has returned.

// The closure (lastName) is called here after the outer function has returned above
// Yet, the closure still has access to the outer function's variables and parameter
mjName ("Jackson"); // This celebrity is Michael Jackson


推荐答案

celebrityName(Michael)(Jackson);

步骤:


  1. celebrityName(Michael)返回函数lastName(theLastName)

  2. (Jackson)传递给函数lastName

  3. 函数lastName(theLastName)在执行时打印字符串

参数从左到右外部到内部调用的方法。

Arguments from left to right go from outer to inner called methods.

这篇关于Javascript闭包函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆