Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for writing javascript widgets [closed]

I have a JS script (widget) which is added to other websites. Technically its similar to Google analytics. My question is: what are your advices for ensuring robustness, keeping the code from interfering with other code on the site, avoiding errors for users etc. In general, what should I know to write a professional grade widget.

Notes: I can't use any JS library such as jquery etc..

like image 878
Nir Avatar asked May 22 '09 20:05

Nir


1 Answers

I am a big fan of Peter Michaux's guide on how he writes javascript widgets

Also useful are Christian Heilmann's script configuration and the module pattern

Those are generic javascript articles and aren't specific to a single library

Other useful tricks are things like wrapping your code in an anonymous function to stop it interfering with other global libraries.

(function() {
    //Your code goes in here
})();

Regarding errors and best practice, John Resig has an interesting article on javascript strict that isn't in yet, but does have some handy information on the sort of things you should be avoiding.

If you're still coming to terms with scoping within your objects, then you might find this article on private and public variables useful as well a a bit more technical definition by Douglas Crockford

Finally, remember to run your completed code through a code quality tool

like image 96
Steerpike Avatar answered Sep 25 '22 08:09

Steerpike