Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > JS library design question (functional vs. OOP)

Reply
Thread Tools

JS library design question (functional vs. OOP)

 
 
petermichaux@gmail.com
Guest
Posts: n/a
 
      09-22-2006
Hi,

I have a general JavaScript library API design question based on some
examples I have seen. There seem to be two options

The first option is used by the AJAX libraries and by Matt Kruse's AJAX
library. These seem like functional programming.

YAHOO.util.connect.asyncRequest('GET', '/the/url',
{success:function(o){alert(responseText)};});
AjaxRequest.get({'url':'/the/url','onSuccess':function(req){
alert(req.responseText);}});

The other option is more OOP and is used by prototype.js for the AJAX
part. This would allow for a more layered library design with
increasing complexity. Perhaps a form request class inheriting from a
non-form request class. I can imagine things like

var request = new Ajax.request('GET', '/the/url',
{success:function(o){alert(responseText)};});
if (request.inProgress()) {}
/* I suppose the request object isn't eligible for garbage collection
even after the request is completed */

-------

Similarly the Yahoo! UI event library uses functional programming like
the following.

function handler(e){alert('event!');}
YAHOO.util.event.addListener('element_id', 'mouseup', handler);
YAHOO.util.event.removeListener('element_id', 'mouseup', handler);

For an event library I can imagine a more OOP style that allows

function handler(e){alert('event!');}
var listener = new Listener('element_id', 'mouseup', handler);
listener.suspend();
listener.resume();
listener.remove();
/* I suppose the listener object isn't eligible for garbage collection
here */

Any words of wisdom or cautions about using the OOP style? It seems
like Yahoo! has gone with the functional style in most cases and this
causes a bit of a mess to keep track of all the different concurent
listeners and requests.

Thank you,
Peter

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      09-24-2006
wrote:
> I have a general JavaScript library API design question
> based on some examples I have seen. There seem to be two
> options
>
> ... . These seem like functional programming.

<snip>
> The other option is more OOP ...

<snip>
> Any words of wisdom or cautions about using the OOP style?
> It seems like Yahoo! has gone with the functional style in
> most cases and this causes a bit of a mess to keep track of
> all the different concurent listeners and requests.


Recently we have seen a link to a definition of "to beg the question". If
someone asks the question "When making an automobile gearbox out of
cheese; is it better to cook the cheese firs or just to leave it to dry
out?" the answer is not one of the two choices offered but rather to
question the rational of making a gearbox out of cheese.

Objects are encapsulations of state and behaviour, usually modelling a
concept. Functions are things that 'act', usually on parameters.
Generally the application would dictate the strategy. If you are working
with concepts that can be modelled as state and behaviour then objects
would be the obvious choice, while a desire to act suggests a function.
With the situation slightly muddied by javascript being a functional
language where functions are actually objects and may be used to
implement 'class' instances.

Rather than letting yourself be directed by someone's opinion of how to
implement things in javascript (to the exclusion of all else) I would
recommend spending some time trying the alternatives out and seeing what
issues follow from them for yourself. To be honest I don't think anyone
starting to learn javascript is going to produce anything worthwhile
without at least 6 months of fairly intensive trial and error experience
(preferably with critical feed-back), and anything published prior to
that is likely to do more harm than good.

Richard.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class design/ design pattern question Bartholomew Simpson C++ 2 06-12-2007 08:51 PM
Design question on wrapping a logging library Dilip C++ 5 05-24-2006 02:19 AM
Design question for UI library xargon C++ 15 12-10-2005 01:34 AM
OO design in servlet design question dave Java 5 07-17-2004 12:58 PM
Xilinx Schematic design vs VHDL code design ZackS VHDL 5 07-09-2004 07:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57