By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,069 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

What is function closure?

P: n/a
?
Jan 27 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.