Friday, September 5, 2008

javascript module pattern...it's what we all should be using

those that know me in the tech world know that i'm an avid ajax developer. i'm as obsessed as i can be about being on the cusp of new tech. and get all giddy when i see any new functionality that i can integrate into my toolbox. that being said, i recently made the switch from prototype to jquery.

there's 3 major reasons for my switch:
1.) jquery is smaller
2.) jquery is faster
3.) jquery has more of a plugin base(prototype doesn't even have plugins) and there are way more things made with jquery.

being an avid prototype user i was heavy into using it's built in classes. a prototypejs class is basically a json object that calls an initialization function when initialized. when i switched from prototytpe to jquery, that had to change.

i read into the jquery forums to see what other people were using, and i found that the most advanced plugins and best implemenations that i could find were using the module pattern.

i started using this in a new site with jquery and i have to say, it's a lot nicer way to program js. it changes the scope so you don't have to worry about binding all of your functions. also, as i read on another site, i'm using two variables for public and private objects (pub and pri) that i prefix onto anything. then all i have to do is return pub and it's all set.

i do have to worry about calling my init function now, but that's ok. 6 extra characters is worth it. check out the basic explaination here:
http://yuiblog.com/blog/2007/06/12/module-pattern/

No comments: