thanks folks, i got it,

On Oct 27, 1:58 pm, Peter van der Zee <jsment...@qfox.nl> wrote:
> On Thu, Oct 27, 2011 at 10:30 AM, Rahul <rahulshivsha...@gmail.com> wrote:
> > hi,
>
> > this is my code,
>
> > 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()();
>
> > 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 ?
>
> > as i am not calling this through window.onload, so how come this
> > works ?
>
> Ok so you have a function that returns a function. Functions are "just
> objects" in JavaScript. So when you return a function, you can
> immediately call it or pass it on. In the event case, you're just
> passing on the returned function to the event handler. In the second
> case you're immediately calling it.
>
> var f = function(){};
> var x = f;
> f();
> f()(); // error
>
> var g = function(){
>   return function(){
>     return 10;
>   };};
>
> g(); // func
> g()(); // 10
> var x =h;
> x(); // func
> x()(); // 10
> var y = h();
> y(); // 10
> y()(); // error
>
> Note that the event handler will simply call your callback when it is 
> triggered.
>
> Hope this helps...
>
> - peter- Hide quoted text -
>
> - Show quoted text -

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to