Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18013
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Two versions of code - advantages and differences |
| Date | 2013-01-07 23:06 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <kcfgto$4hj$1@speranza.aioe.org> (permalink) |
| References | <kc4uud$fam$1@speranza.aioe.org> <1512443.gQHv90F1im@PointedEars.de> <kccqct$ksk$1@speranza.aioe.org> <1603326.gHQpjbgioL@PointedEars.de> |
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.
[...]
>> Yes, but the examples are really simple and I wanted to demonstrate some
>> general idea. The "el" doesn't have to be always an reference to object.
>> This could be anything and could be more complex.
>
> I was pointing out a general problem with self-calling functions, using an
> example.
Indeed.
>> I've just wrote something like this:
>>
>> var main = {
>> [...]
>>
>> apply : (function(){
>> var tempImg = document.createElement('img'),
>> t, img;
>> tempImg.width = '10';
>> tempImg.height = '10';
>>
>> return function(o){
>> img = tempImg.cloneNode(false);
>> img.alt = o.alt;
>> img.src = o.src;
>> t = document.createElement('span');
>> t.appendChild(document.createTextNode('\u00a0'));
>> t.appendChild(img);
>> t.appendChild(document.createTextNode('\u00a0'));
>> window.setTimeout(function(){ examplefn(t); }, 100);
>> };
>> }
> ^^
> JFYI: This is not going to work.
Sorry, fast typing. A typo.
>> [...]
>> };
>>
>> As I understand (correct me if I am wrong) this is inefficient because:
>>
>> * closure need extra memory and will be always in memory because
>> returned function refers to variables that are outside of returned
>> function.
>>
>> * "apply" is parsed immediately which is not needed always
>>
>> Anything else inefficient or wrong?
>
> 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.
> I would have written
>
> apply: function (o) {
> var img = document.createElement('img');
> img.width = 10;
> img.height = 10;
> img.alt = o.alt;
> img.src = o.src;
>
> var t = document.createElement('span');
> t.appendChild(document.createTextNode('\u00a0'));
> t.appendChild(img);
> t.appendChild(document.createTextNode('\u00a0'));
> window.setTimeout(function() { examplefn(t); }, 100);
> }
Yes. After some discuss in this topic I would change it to a simpler
version.
> here, and further optimized (with regard to maintenance effort and
> compatibility) to
>
> apply: (function () {
> var _createElementFromObj = jsx.dom.createElementFromObj;
> var _runAsync = jsx.dom.timeout.runAsync;
>
> return function (o) {
> var t = _createElementFromObj({
> type: "span",
> childNodes: [
> "\u00a0",
> {
> type: "img",
> properties: {
> width: 10,
> height: 10,
> alt: o.alt,
> src: o.src
> }
> },
> "\u00a0"
> ]
> });
>
> _runAsync(function () { examplefn(t); }, 100);
> };
> }())
>
> (Wrappers like that are functionally optional of course, but I find them
> very useful. Although 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).
> There are times when extra closures are a good idea, and there are times
> when they are not. There is no definitive answer here, but my code should
> give you some idea.
I think that I created little bit overcomplicated my code and now I
rewrite it in a simpler version. No need extra closure in that case,
like mine.
--
Cezary Tomczyk
http://www.ctomczyk.pl/
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