jQuery's Object Literal Coding Conventions
jQuery has internal coding conventions. It uses tabs over spaces. It uses lots of white paces and you can always find a {} around if/else statements. I'm adopting these conventions for JavaScriptMVC. JavaScriptMVC had notoriously flexible (ie bad) coding conventions. We were more concerned with adding features, tests, etc than making our code look nice. But, for the 3.0 release, the features have stabilized. We're paying more attention to things like documentation and even coding conventions.
So, I spent a few hours today making JavaScriptMVC code look like jQuery code with a few simple regular expressions. My goal was to make our Object literals look like:
httpSuccess: function( xhr ) {
One nice feature you may not know is that you can search the jQuery source for a method easily by searching for METHOD:. I like that. So, I used a few regular expressions to convert our varied object literal methods to jQuery style object literal methods. Here's what they do in order you should run them:
//Converts method : function -> method: function
RegExp =/(\w+)\s*:\s*function/
Replace ="$1: function"
//Converts : function(f -> : function( f
RegExp =/: function\(([^ \)])/
Replace =": function( $1"
//Converts : function( f){ -> : function( f ) {
RegExp =/: function\(([^\)]*)([^ ])\)\s*\{/
Replace =": function($1$2 ) {"
//Converts : function(){ -> : function() {
RegExp =/: function\(\)\s*\{/
Replace =": function() {"
I'm positive there's an easier way to do this, but this might get you started cleaning up your own code. Maybe someday we'll roll this into JavaScriptMVC.
Subscribe to:
Related Content
About Jupiter
Jupiter is dedicated to making JavaScript an easy and enjoyable place to develop kick ass apps. We open-source everything and provide expert web application development, support, and training.
Recent Posts
- Significant Whitespace
- 3.2 $.Controller - Templated Event Binding
- Deferreds and 3.1
- jQuery Resize Event
- FuncUnit Humor
- Having your Cake and Eating it without Getting Fat
- Why You Should Never Use jQuery Live
- Knock JavaScriptMVC's Back Out
- Advanced jQuery Training at SF jQuery Conf 2011
- JavaScriptMVC and List Performance
JavaScript development, design, and consulting.