Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Gregor Kofler Newsgroups: comp.lang.javascript Subject: Re: Two versions of code - advantages and differences Date: Fri, 04 Jan 2013 11:25:06 +0100 Organization: A noiseless patient Spider Lines: 99 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 4 Jan 2013 10:25:06 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="8704bf164e793a947cd84ce3d228347f"; logging-data="11362"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iEg8kXCWkgzWQZgg7eH+aHpufHW+iYRw=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: Cancel-Lock: sha1:JCoAD7ua2qF5DADdKxfr71RCREk= Xref: csiph.com comp.lang.javascript:17937 Am 2013-01-04 09:15, Cezary Tomczyk meinte: > W dniu 2013-01-04 00:41, Stefan Weiss pisze: >> 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. > > As I mentioned the examples is just a simple examples. Yes, they are > trivial, but just wanted to show general idea. They can contains more > complicated code. > >> 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'. > > But same can be done in Version 1. At the cost of requiring more non-private variables. >> - When, depending on the availability of 'el', two different functions >> will be returned. > > That's true, but this can be done also in Version 1. Functions can be > defined and returned. When you start returning functions, then you end up with version 2. >> - When it uses an internal state (for example, a counter). > > This also can be done in Version 1 and I do not see advantage here over > Version 2. See above. >> - When it uses private helper functions that are useless in the >> surrounding scope. > > Yes. This is the advantage Version 2 over Version 1. Of course, in some > cases and it depend on what is required. > >> In general, if the same goal can be achieved in a simple and a >> complicated way, choose simple :) > > I agree :-) Just wanted to get some confirmations if I am thinking in > the right way ;-) > >> 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. > > You mean this? > http://stackoverflow.com/a/3344397 As long as you understand the differences between the two you can use both (or prefer whatever meets your personal preference). Gregor