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;
}();
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".
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()
."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()
.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.undefinded
is returned from statementsvar a = ...
that is OK. Reference is returned by the right side of assigment.