admin管理员组文章数量:1296237
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 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;
}();
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 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;
}();
Share
Improve this question
edited Oct 24, 2012 at 16:44
hippietrail
17k21 gold badges109 silver badges178 bronze badges
asked Mar 20, 2011 at 10:48
LeahcimLeahcim
42k61 gold badges203 silver badges343 bronze badges
1
-
The function returns another function which contains the alert. So after calling your outer function, it will resolve to
actualWork
being set toa
. When you then calla
, you basically call theactualWork
function, i.e. then you get the alert. – pimvdb Commented Mar 20, 2011 at 11:11
5 Answers
Reset to default 4This code is equivalent to:
var f = function() {
function someSetup(){
var setup = 'done';
}
function actualWork(){
alert('Worky-worky');
}
someSetup();
return actualWork;
};
var a = f();
So: You are creating variable f
and assigning a function to it. Then You assign the result of invoking f()
to a
, which happens to be a function as well (this is done implicitly in the original code). And at the very end you can run a()
, which runs the the function returned by f()
.
"Self-invoking" means that this code has an immediate effect immediately after it is executed by the piler, 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
function someSetup(){
var setup = 'done';
}
function actualWork(){
alert('Worky-worky');
}
someSetup();
return actualWork;
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
and actualWork
visible to the calling code -- this is generally desirable.
With that explained, let's answer your questions:
- The code snippet does not
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
. - The reference to function
actualWork
is returned fine (it's the value assigned toa
); you saw that yourself when you successfully called it witha()
.
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:
- define 2 other (temporary) functions
- run one of them (someSetup)
- return the other one.
so i would use the program by:
- running to define a();
- calling
var x=a();
- (optional) checking if
setup=='done'
- calling
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
is returned from statements var a = ...
that is OK. Reference is returned by the right side of assigment.
The anonumous function is invoked immediately and returns a reference to the function actualWork
within it. So a
contains this reference and can be invoked by itself. So a();
should give you the alert.
本文标签: iifeJavaScript selfinvoking functionStack Overflow
版权声明:本文标题:iife - JavaScript self-invoking function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741634471a2389561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论