Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.unit0.net!takemy.news.telefonica.de!telefonica.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1512443.gQHv90F1im@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 04 Jan 2013 15:31:21 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 71 NNTP-Posting-Date: 04 Jan 2013 15:31:22 CET NNTP-Posting-Host: c735a074.newsspool3.arcor-online.net X-Trace: DXC=^Z6]ac3iL5RYQ5E:lYBYU3JEo]QCo[:6bBE?Q X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:17946 Cezary Tomczyk wrote: > I have a two versions of code. They are just only examples and contains > simple operations, but I want to understand more deeply some general > things. > > 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 > } > }()); Your indentation looks partially bad here. Use spaces, not tabs. > […] > Anything else what can be said about advantages or differences between > them? Static code analysis has a hard(er) time recognizing that “fn” actually refers to a function in Version 2. AFAIK, the JSDoc Toolkit cannot deal with it at all (but my JSdoc is going to). Another advantage of Version 1 over Version 2 is that it does not matter if “el” is initialized, or its initialization value is available, before or after the definition. For example, you would not want to use Version 2 in a library that is loaded before the document has been loaded, because ”el” will be a false-value then. The document need not have been parsed to after the element in question, and the document tree not been populated as much, before the document has been loaded. If you skip the initialization of “el” in Version 1, you can load the code and still initialize “el” later, when appropriate. Your should not simply return in the second branch in Version 2, because calls will fail then; you should return a reference to a Function instance with an empty function body. Incidentally, this code can then be simplified to var fn = (function () { var el = document.getElementById('test'); return function () { return el; }; }()); where it becomes obvious that the additional closure is not really needed here, and the function itself is unnecessary. Perhaps you should present better examples. PointedEars -- When all you know is jQuery, every problem looks $(olvable).