JavaScript自调用函数
我从 Stoyan Stefanov 所著的《面向对象 JavaScript》一书中的第 79 页得到了这个示例。我真的不知道该怎么做,第一次运行这个程序(按回车键)它返回“未定义”。之后,按照作者的指示,我将其命名为 a();
并收到警报“Worky worky”
我的问题是
a) 我是否正确执行了第一步?即我是否应该仅通过点击“输入/返回”来运行自调用程序?
b)如果我是正确的,只是点击“输入/返回”来运行程序,为什么它给出的结果是“未定义”。作者说,这个程序(在第一次运行时)拒绝对函数actualWork()的引用?如果它返回一个引用,为什么它被认为是未定义的?它在某种程度上重要吗?
请注意,我尝试在 jsfiddle.net 中输入代码,然后点击运行,但没有发生任何事情,但是当我第一次在控制台中运行它时,我得到了“未定义”,然后当我执行 a(); 时出现警报;
var a = function() {
function someSetup(){
var setup = 'done';
}
function actualWork(){
alert('Worky-worky');
}
someSetup();
return actualWork;
}();
I got this example from page 79 of a book called Object Oriented JavaScript by Stoyan Stefanov. Not really knowing what to do, the first time I ran this program (by hitting enter) it returned 'undefined'. After that, following the author's instructions, I called it a();
and got the alert 'Worky worky'
My questions are
a) Did I do the first step correctly? i.e. am I supposed to run a self-invoking program by merely hitting "enter/return"?
b) if I was correct to just hit "enter/return" to run the program, why did it give a result of "undefined." This program, says the author, refuns a reference (on its first run-through) to the function actualWork() ? if it returns a reference, why is that considered undefined? Is it important somehow?
Note that I tried to enter the code in jsfiddle.net and then hit run and nothing happened, but that I got "undefined" when I ran it the first time in the console and then the alert afterwards when I did a();
var a = function() {
function someSetup(){
var setup = 'done';
}
function actualWork(){
alert('Worky-worky');
}
someSetup();
return actualWork;
}();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
此代码相当于:
因此:您正在创建变量
f
并为其分配一个函数。然后将调用f()
的结果分配给a
,它恰好也是一个函数(这是在原始代码中隐式完成的)。最后,您可以运行a()
,它运行由f()
返回的函数。This code is equivalent to:
So: You are creating variable
f
and assigning a function to it. Then You assign the result of invokingf()
toa
, which happens to be a function as well (this is done implicitly in the original code). And at the very end you can runa()
, which runs the the function returned byf()
.“自调用”意味着该代码在被编译器执行后立即生效,即使所有代码都在函数内部。发生这种情况是因为该函数被立即调用(最后一行中的那些
()
)。如果您只是编写,这也是会发生的情况。
这里的区别在于,“自调用程序”(恕我直言,这是对其含义的错误描述)允许您在不使用名称
someSetup< 的情况下执行此操作/code> 和
actualWork
对调用代码可见——这通常是可取的。解释完后,让我们回答您的问题:
返回
任何内容(即使它分配给a
返回的内容一个函数);因此,您的 JS IDE 报告其返回值为undefined
。actualWork
的引用可以正常返回(它是分配给a
的值);当您使用a()
成功调用它时,您自己就会看到这一点。"Self-invoking" means that this code has an immediate effect immediately after it is executed by the compiler, even though all the code is inside a function. This happens because the function is immediately invoked (those
()
in the last line).This is also what would have happened if you wrote just
The difference here is that the "self-invoking program" (which is a bad description for what it is, IMHO) allows you to do this without making the names
someSetup
andactualWork
visible to the calling code -- this is generally desirable.With that explained, let's answer your questions:
return
anything itself (even if it assigns toa
something that is returned by a function); therefore your JS IDE reports that its return value isundefined
.actualWork
is returned fine (it's the value assigned toa
); you saw that yourself when you successfully called it witha()
.a) 取决于你的解释器的工作方式,是的,只需运行脚本(按回车键)即可定义函数 a();
b) 它给出“未定义”,因为程序本身不返回任何内容,但函数 a();做。
上面引用的代码代表一个函数 a();当调用时,应该执行以下操作:
所以我会通过以下方式使用该程序:
var x=a();
setup=='done'
是否x();
(由a()) 应显示警报。编辑
抱歉,我没有看到}();最后,所以它不是100%正确的。这 ();最后是 - 正如 Jon 和 Tomasz 所说 - 缩写形式“将函数的返回值作为新函数并立即运行它”。
a) depending on how your interpreter works, yes, simply running the script (by pressing return) defines function a();
b) it gave "undefined" because the program itself doesn't return anything, but the function a(); does.
the code you quoted above represents a function a(); which, when invoked, should do the following:
so i would use the program by:
var x=a();
setup=='done'
x();
(which was returned by a()) should show the alert.EDIT
sorry, i didn't see the }(); at the end, so it's not 100% correct. the (); at the end is - as Jon and Tomasz both said - short form for "take the function's return value as a new function and run it immediately".
undefinded
从语句var a = ...
返回,这是可以的。引用由赋值的右侧返回。undefinded
is returned from statementsvar a = ...
that is OK. Reference is returned by the right side of assigment.匿名函数会立即被调用,并返回对其中函数
actualWork
的引用。所以a
包含这个引用并且可以被它自己调用。所以a();
应该给你警报。The anonumous function is invoked immediately and returns a reference to the function
actualWork
within it. Soa
contains this reference and can be invoked by itself. Soa();
should give you the alert.