Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #18012
| From | Cezary Tomczyk <cezary.tomczyk@gmail.com> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: Two versions of code - advantages and differences |
| Date | 2013-01-07 22:53 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <kcfg47$31q$1@speranza.aioe.org> (permalink) |
| References | <kc4uud$fam$1@speranza.aioe.org> <04c4a734-68df-451c-9caa-8a9d93ccb9da@f8g2000yqa.googlegroups.com> <kcbiha$f20$1@speranza.aioe.org> <7598b8a8-0d44-43a6-9b8e-f1657068a484@w8g2000yqm.googlegroups.com> |
W dniu 2013-01-06 21:56, David Mark pisze:
> On Jan 6, 5:09 am, Cezary Tomczyk <cezary.tomc...@gmail.com> wrote:
>> W dniu 2013-01-05 18:13, David Mark pisze:
>> [...]
>>
>>> var fn;
>>> var el = ...
>>
>>> if (el) {
>>> fn = ...
>>> }
>>
>>> Now, before you start another bit of that *requires* the "fn"
>>> function, make sure it exists:
>>
>>> if (fn) {
>>> ...
>>> }
>>
>> Yes, this is a "well-known style" that I know from My Library which I
>> mostly use. :-)
>
> I think everybody should be using it at this point. Doesn't take long
> to figure out it is the only simple way to keep things straight in a
> cross-browser script (as well as any plug-ins) of any real depth.
> Otherwise, even basic tasks like enabling command buttons at the
> outset (once the required API functions for each are detected) becomes
> a nightmare. And, of course, presenting a toolbar with buttons that
> call empty functions (or that break as a result of calling empty
> functions) is not going to please anyone.
I didn't thought really about empty function as an alternative in case
if some test of feature is not passed :-) That was just an example, but
I need to be more strictly in the future. Posting just code and thinking
that readers have a "crystal ball" to guess what I mean is not a good
idea :-)
>> As for above code: maybe it is better to catch exception like this
>> (example):
>>
>> if (fn) {} else {
>>
>> throw "No method fn";
>>
>> }
>
> You could for debugging purposes, but then the exception thrown by the
> browser will be similar. I've taken to calling such bonus code
> "scaffolding" and the Jessie builder removes it for production (by
> which time you should have encountered and corrected such oversights).
Maybe in some cases "throw" can send some "event" to tracking system
about that. So, then errors can be easy tracked by any system like
Google Analytics or Adobe Omniture or something similar.
>> Of course, I understand that not all scenarios needs this.
>> However, sometimes it is better to make a closure to hide some variables
>> and implementations that is not available on the outside. The questions
>> is when is is really needed?
>
> One API function should not be able to change "members" of another.
Generally, I agree.
>>
>> Example from MyLibrary:
>>
>> var elementUniqueId = (function () {
>> var it = 0;
>> return function (el) {
>> return el.uniqueID || (el.uniqueID = '_api' + it++);
>> };
>>
>> })();
>>
>> Why it can not be as a:
>>
>> var elementUniqueId, it = 0;
>>
>> elementUniqueId = function (el) {
>> return el.uniqueID || (el.uniqueID = '_api' + it++);
>>
>> })();
>
> Other than the typo at the end, that's fine. However, other functions
> could change the "it" variable, which would foul up this function.
> That's why you use the module pattern in this case.
Ok
>> The closure here is probably because there is a risk of overwrite
>> variable "it", right?
>
> Exactly.
:-)
>> But disadvantage of this is that closure needs
>> extra memory and as I suppose it is not efficient. Correct me if I am wrong.
>
> It will use extra memory and in My Library there are cases where this
> is done for no good reason. There are also cases where functions could
> potentially foul up the works of others. Libraries churned out by the
> Jessie builder do not have either of these issues as I defined strict
> authoring rules (not to be confused with "use strict" of course) from
> the outset.
I am watching Jessie, also :-)
--
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