469,907 Members | 2,178 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,907 developers. It's quick & easy.

What is function closure?

?
Jan 27 '08 #1
4 1861
function your_function()
{
var closure='hello function';
return function()
{
alert(cosure);
}
}

On Jan 27, 4:24 pm, LAN MIND <LANMIN...@gmail.comwrote:
?
Jan 27 '08 #2
sorry, that one line should be:
alert(closure);

That should answer your question if it were about how to make a
closure. If your question was actually asking what it was (in a what
does it do sense), then a closure is a function that has access to
various temporary variables that were created in the same scope that
they were (see example below).

On Jan 27, 7:26 pm, "liamge...@gmail.com" <liamge...@gmail.comwrote:
function your_function()
{
var closure='hello function';
return function()
{
alert(cosure);
}

}

On Jan 27, 4:24 pm, LAN MIND <LANMIN...@gmail.comwrote:
?
Jan 27 '08 #3
From the "javascript: The Definitive Guide" book:

«JavaScript functions are a combination of code to be executed and the
scope in which to execute them. This combination of code and scope is
known as a closure (...) All JavaScript functions are closures. These
closures are only interesting, however, in the case discussed above:
when a nested function is exported outside the scope in which it is
defined. When a nested function is used in this way, it is often
explicitly called a closure.»
Jan 27 '08 #4
On Jan 27, 3:01 am, Marcio Faustino <m.faust...@gmail.comwrote:
From the "javascript: The Definitive Guide" book:

«JavaScript functions are a combination of code to be executed and the
scope in which to execute them. This combination of code and scope is
known as a closure (...) All JavaScript functions are closures. These
closures are only interesting, however, in the case discussed above:
when a nested function is exported outside the scope in which it is
defined. When a nested function is used in this way, it is often
explicitly called a closure.»
One common use is when SetTimeout is called. Suppose you set up a
timer which will do something in 10 seconds. You don't have to worry
about keeping the scope around to be used because the closure holds on
to the scope long enough for the SetTimeout to use it.
Jan 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Jonas Kölker | last post: by
2 posts views Thread by Fred Bassett | last post: by
3 posts views Thread by ashok | last post: by
12 posts views Thread by salonowiec | last post: by
12 posts views Thread by Ziggy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.