On Thu, Oct 27, 2011 at 10:30 AM, Rahul <[email protected]> wrote:
> function fngg(){
>   var f = function(){ alert(studentName); }
>   var studentName = "Kate Adam Bikensale";
>   return f;
> }
>
> now i can call the above function using window's onload event as
> follows,
>
> window.onload = fngg();
>
> this works fine,
>
> but how come the following code works
> fngg()();

Why shouldn't it?
The returned function gets called immediately, not when the page has
loaded, but otherwise it should do the same thing.

> just when i refresh the page the fngg function gets called as well as
> the inner closure "f" is also getting called,
>
> my question is on which event the "fngg()()" is linked to ?

The "event" of evaluating the script element that the code is included in.
It's the same time the assignment to window.onload would normally
happen - that's just Javascript code being executed too.

> as i am not calling this through window.onload, so how come this
> works ?

There is nothing in your returned function that requires it to wait
for the onload event, so it's perfectly fine to call it earlier, which
is what you do.

/L

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to