[英]Why assign a declared function to a variable in javascript?
I'm trying to learn about closures and came across this bit of code from MDN. 我正在尝试了解闭包,并从MDN遇到了这段代码。 I tried it in jsbin and it works, but I can't figure out why it's necessary to assign makeFunc to var myFunc and then calling myFunc rather than just calling makeFunc, which doesn't work. 我在jsbin中进行了尝试,并且可以正常工作,但是我无法弄清楚为什么必须将makeFunc分配给var myFunc然后再调用myFunc而不是仅仅调用makeFunc,这是行不通的。
function makeFunc() {
var name = "Mozilla";
function displayName() {
alert(name);
}
return displayName;
}
var myFunc = makeFunc();
myFunc();
I can't figure out why it's necessary to assign makeFunc to var myFunc 我不知道为什么需要将makeFunc分配给var myFunc
That isn't what is happening. 那不是正在发生的事情。 Look at the code: 看一下代码:
var myFunc = makeFunc();
^^
makeFunc
is being called . makeFunc
被调用 。 It is its return value that is assigned to myFunc
. 它的返回值分配给myFunc
。
That return value is displayName
(which has access to the closed over name
variable). 该返回值是displayName
(可以访问封闭的name
变量)。
myFunc();
then calls that function. 然后调用该函数。
makeFunc
is returning a function called displayName
which includes the value of "Mozilla" within the variable name
which is only accessible within the displayName
function (although it doesn't "know" that it was originally called "displayName"...it just simply now contains the body of that function along with the value of name
). makeFunc
返回一个名为displayName
的函数,该函数在变量name
包含“ Mozilla”的值,该值只能在displayName
函数中访问(尽管它不“知道”它最初被称为“ displayName” ...只是简单的现在包含该函数的主体以及name
的值)。 When you assign the result of calling makeFunc
to myFunc
, myFunc
contains the function displayName
, including the value of name
. 将调用makeFunc
的结果分配给myFunc
, myFunc
包含函数displayName
,包括name
的值。 Note, however, that displayName
has not yet at that point in time been called/invoked. 但是请注意,此时尚未调用displayName
。 Now, when you invoke myFunc
, displayName
is run, creating an alert showing the name "Mozilla". 现在,当您调用myFunc
,将运行displayName
,创建一个显示名称“ Mozilla”的警报。
On page load closure allows a function to run automatically 页面加载关闭允许功能自动运行
FOR YOUR CODE -- you can re-adjust your code to this like you anticipated without assigning it to variable myFunc 对于您的代码-您可以按预期将其重新调整为该代码,而无需将其分配给变量myFunc
(function makeFunc() {
var name = "Mozilla";
function displayName() {
alert(name);
}
return displayName;
})();
I'll try to give a very short answer: 我将尝试给出一个简短的答案:
makeFunc()
only returns the function without executing it because the return value is the name of the function. makeFunc()
仅返回函数而不执行它,因为返回值是函数的名称。
var myFunc = makeFunc();
myFunc();
From the code above you're assigning the function that I previously mentioned to myFunc
. 在上面的代码中,您正在将我先前提到的功能分配给myFunc
。 On the next line you add a ()
which means execute the function. 在下一行添加()
,表示执行该函数。 Hence why you see the alert();
因此,为什么要看到alert();
result. 结果。
makeFunc() == displayName
myFunc == makeFunc()
myFunc == displayName
myFunc() == displayName()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.