Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!newsfeed.straub-nv.de!noris.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="UTF-8" Message-ID: <1718550.E62eDaYUNW@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Tue, 08 Jan 2013 12:35:29 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 8Bit Subject: Re: Two versions of code - advantages and differences Newsgroups: comp.lang.javascript References: <1512443.gQHv90F1im@PointedEars.de> <1603326.gHQpjbgioL@PointedEars.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 79 NNTP-Posting-Date: 08 Jan 2013 12:35:29 CET NNTP-Posting-Host: 39efd3a3.newsspool3.arcor-online.net X-Trace: DXC=]eiY5Rm9C7K[6=1B@oB@@@McF=Q^Z^V3H4Fo<]lROoRA8kFYBYU3JEoM:]c;j=1VF>M X-Complaints-To: usenet-abuse@arcor.de Xref: csiph.com comp.lang.javascript:18019 Cezary Tomczyk wrote: > W dniu 2013-01-07 04:36, Thomas 'PointedEars' Lahn pisze: >> Cezary Tomczyk wrote: > [...] >>>> 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 >>>>> } >>>>> }()); > [...] > >>>> 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). >>> True, but they are two different examples. >> Your point being? > > I was thinking that even if the functions are different then variable > name is the same. I thought that JSdoc will catch this, but this doesn't > make sense. When both of functions will be used in the same scope then > Version 2 will overwrite Version 1. Not all functions need be documented, so a documentor should consider the documentation for a function to be finished when it sees the next “function” keyword at the same nesting level. However, an exception needs to be made for extra closures, because the outer function that returns the actual function is not the function, and its parameter list does not contain the parameters, that you usually want to document. Yet the identifier is assigned the return value of that outer function, and you do want to document the identifier if it is globally available or forced by the developer to be documented. >> Your code does not make sense to me at all, regardless of possible >> inefficiencies. You are creating on initialization an ”img” object only >> to clone it non-recursively (assuming this works) when the returned >> function is called only to skip the “width” and “height” assignment – >> seriously? > > I made a test and seems that creating once img node with properties and > clone them later every time is faster than creating new img node and > adding new properties. Just a small optimization. Even if that was so (never trust benchmarks), it adds a dependency on Node::cloneNode(). >> […] it escapes me here why you would want to insert >> the equivalent of “ ” before and after the image; this should be >> done with the “margin” CSS property instead.) > > Tried to use CSS, but it doesn't work for img. See: > http://jsfiddle.net/w7BAu/ > > Tested on Firefox 17.0.1 and IE9 (Windows 7, 64 bit). AISB, use the “margin” property. -- PointedEars