Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16120
| From | Stefan Weiss <krewecherl@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: silly question on closure |
| Date | 2012-09-22 18:18 +0200 |
| Organization | albasani.net |
| Message-ID | <k3kocg$8ok$1@news.albasani.net> (permalink) |
| References | <a9704993-d83a-4392-8a65-82755e9ba320@u15g2000yql.googlegroups.com> |
On 2012-09-22 14:04, justaguy wrote:
> Code:
[snipped]
> Question:
> The closure takes so many lines while the function has only 2 lines to
> get the same thing done. So, the question is, why do we use closure?
You don't, just like you don't actually use "Hello, world" in production
code. This was obviously just an example for how closures work. If this
was code intended for actual use, your "just a function" variant would
be better suited (provided it only returned the value, like in the
"closure" example).
One case where the closure variant would be preferred is if the contents
of then "names" array would take a long time to compute, or if they were
fetched from a remote source.
> Could you elaborate a bit with a practical example?
Counters are popular examples for simple closures:
var sequence = (function () {
var value = 0;
return function () {
++value;
return value;
};
})();
alert(sequence()); // 1
alert(sequence()); // 2
alert(sequence()); // 3
- stefan
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 05:04 -0700
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-22 18:18 +0200
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 18:41 -0700
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 01:10 +0200
Closures (was: silly question on closure) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 13:14 +0200
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 05:04 +0200
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-27 20:50 +0200
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 21:29 +0200
Re: silly question on closure "Tom de Neef" <tdeneef@qolor.nl> - 2012-09-22 20:00 +0200
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 18:42 -0700
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 07:31 -0700
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-23 18:49 +0200
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 13:33 -0700
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 13:38 -0700
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 20:49 +0200
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 01:14 +0200
Re: silly question on closure dann90038@gmail.com - 2012-09-23 15:37 -0700
csiph-web