473,396 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JS library design question (functional vs. OOP)

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

Sep 22 '06 #1
1 1525
pe**********@gmail.com 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.
Sep 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
7
by: Sonny | last post by:
I need to port a library that is written entirely in C to C++. The library is supported on quite a few platforms (windows, Solaris, Linux, AIX, HP-UX, OSX, etc...) and there's quite an existing...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
29
by: Brad Pears | last post by:
Here is a simple OO design question... I have a Contract class. The user can either save an existing contract or they start off fresh with a blank contract, fill in the data and then save a...
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
11
by: josh | last post by:
Hi, I read a lot on how to make in Javascript programs in a OOP way. After I download many third-party library (like YUI, prototype ecc.) and I have seen that almost all the function (class) are...
7
by: Immortal Nephi | last post by:
I have an idea how to design an object in a better way. I would like to give you my thought example. Please let me know what you think if it is best object design. Please recommend me any book...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.