Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #17927
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.albasani.net!.POSTED!not-for-mail |
|---|---|
| From | Stefan Weiss <krewecherl@gmail.com> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: Two versions of code - advantages and differences |
| Date | Fri, 04 Jan 2013 00:41:06 +0100 |
| Organization | albasani.net |
| Lines | 69 |
| Message-ID | <kc54ui$hvr$1@news.albasani.net> (permalink) |
| References | <kc4uud$fam$1@speranza.aioe.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.albasani.net YkneCwqO76oafKneWMvcAmdXcYaUtbs/vpmsjmL8KStHLtD3HbmIpl/651ytecFRMXN88a+NkEKWhV7mG1WIqFOoZLVrbxuL+yr6ZWP9r7Xlzl4BojHQ51JwxUQy/f46 |
| NNTP-Posting-Date | Thu, 3 Jan 2013 23:41:06 +0000 (UTC) |
| Injection-Info | news.albasani.net; logging-data="ZhTEfJmQM8Y8SolxRTdSUo9vWvQwiWRMaq7IjVjpZ0RQDJQOk8/xjfUbU3bG2pVNH6uNDHUp8u9Gchzjo+4S0n7+7a5W2bDbSFj5bTSdqgu3e9q258RebmvbNhdq8fM+"; mail-complaints-to="abuse@albasani.net" |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 |
| In-Reply-To | <kc4uud$fam$1@speranza.aioe.org> |
| Cancel-Lock | sha1:pCXg6VjfInLNae7nt9P3lKctRiY= |
| Xref | csiph.com comp.lang.javascript:17927 |
Show key headers only | View raw
On 2013-01-03 22:58, Cezary Tomczyk wrote:
> Version 1
>
> var el = document.getElementById('test');
> var fn = function(){
> if( !el ){
> // fallback if el is not available and then return
> }
> return el;
> };
>
> Version 2
>
> var fn = (function(){
> var el = document.getElementById('test');
>
> if(el){
> return function(){
> return el;
> }
> } else {
> // fallback if el is not available and then return
> }
> }());
>
> Correct me, if I am wrong.
>
> a) Version 1 has an advantage over Version 2, because Version 2 is
> slower (?) and consumes more memory (?).
> b) Version 2 has a closure and inside fn everything is not available
> outside (except what return "return").
Your interpretation is broadly correct, but the example is too trivial
to make a good distinction (especially concerning performance or
memory). If that's all there is to it, version 1 would usually be
preferred for its simplicity.
There are several situations where a closure like version 2 would be
better suited. For example:
- When the function performs something computationally expensive (or
taking a long time), the result will be "remembered" and available for
subsequent calls to 'fn'.
- When, depending on the availability of 'el', two different functions
will be returned.
- When it uses an internal state (for example, a counter).
- When it uses private helper functions that are useless in the
surrounding scope.
In general, if the same goal can be achieved in a simple and a
complicated way, choose simple :)
By the way, one way of keeping it simple would be to use
function fn () {}
instead of
var fn = function () {};
for version 1.
Some people recommend to always use function expressions, but IMO, if
you know how variable and function declarations are handled when an
execution context is entered, this is unnecessary in most cases.
- stefan
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-03 22:58 +0100
Re: Two versions of code - advantages and differences Stefan Weiss <krewecherl@gmail.com> - 2013-01-04 00:41 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 09:15 +0100
Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 11:26 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 14:29 +0100
Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 14:56 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 16:52 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 09:19 +0100
Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 11:25 +0100
Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-04 15:31 +0100
Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 08:37 -0800
Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-05 18:14 +0100
Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 10:51 -0800
Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-05 20:46 +0100
Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 17:12 -0800
Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 17:43 -0800
Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 03:58 +0100
Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 19:23 -0800
Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 05:19 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 22:30 +0100
Re: Two versions of code - advantages and differences Stefan Weiss <krewecherl@gmail.com> - 2013-01-07 02:35 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 22:45 +0100
Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 04:36 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 23:06 +0100
Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-08 12:35 +0100
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-09 08:34 +0100
Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 09:13 -0800
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 11:09 +0100
Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 12:56 -0800
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 22:53 +0100
Re: Two versions of code - advantages and differences Luc Yen <luc@goal.tw> - 2013-01-05 12:54 -0800
Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 21:38 +0100
csiph-web