All of my projects include very similar tasks and quite recently I’ve been thinking I should write a library to handle most of the heavy lifting so I can write short, simple, easy to read code that interacts with the library to get the jobs done. Looking at other frameworks, specifically jQuery & YUI, they work in different ways. jQuery mostly seems to extend & simplify the DOM using a global object/function whereas YUI uses namespaces and modules. I understand that this is because they have different goals as libraries and each have their merits.
Most specifically, I’m looking for answers to the following questions:
- Is it best to use namespaces, eg
gui.scrollbar.attach()
, or a global method such as$(domObj).attachScrollbar()
? - Is it best to include individual parts as seperate js files or as a single js file where components are copied/pasted? I know the eBay Shopping API SDK has many .js files in seperate folders, wouldn’t this suffer a significant performance hit?
- Is there any other advice you would give to someone looking to create a javascript library?
I’ve marked as community wiki because I didn’t want to risk the question being closed – I’m genuinely seeking advice here.
Thanks
EDIT
I should have originally stated that I’m not re-inventing the wheel, I’m looking to simplify many common tasks between Windows Desktop Gadgets, so I don’t need the cross-browser compatibility of other libraries and their core functionality doesn’t really apply to what I’m doing. I tried to keep the question general and not specifically relating to desktop gadgets, so that this question would be useful for others.
3
Answers
Is there any reason why you can’t use one of the many libraries out there already? This seems like a case of reinventing the wheel. These libraries have been around for years and have already tackled many of the issues that you will surely run into when trying to write your own.
In terms of some of your questions:
Having said all that, I restate that you should look at using one of the libraries already out there unless you have a very good reason not to. Keep in mind that you will need to test in all old browsers as well as any new browsers that come out.
jQuery, YUI, Prototype, Mootools, extJS are a few of the most popular
Simple answer to your question: write your library using an existing one.
I have some javascript files written in jQuery just for such purposes.
More specifically,
a. you avoid the overhead you mentioned
b. you prevent unnecessary dependencies creeping up externally.
Cheers!
Concerning your first question, the two options are really the same IMO. Instead of a global object like YAHOO or gui, you have a global function like $. In both cases you have a global variable that holds your namespace.