Contact the core Jetty developers at
www.webtide.com
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ...
scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

Jetty Java Coding Standards

Javascript coding standards

With Ajax, we are using Javascript as a first class programming language, not just as a value-ad to html mark up. Thus it is very important that we establish good coding practises. The following are some key points to how we wish to use javascript in the ETrade project:

Use objects rather than functions.

traditional javascript makes extensive use of functions. Where ever possible these functions should be grouped together as an Object. Thus instead of writing:

we should write:

Private fields and functions should have names starting with '_'.

Note that this is a very light weight form of object and we are not using any concept of class. It is equivalent to a javaclass of static members and simply groups related functions together for name space clarity.

Javascript also supports classe via the prototype mechansim. This should also be used when appropriate but is not covered by the example above.

JS files as packages

Just as Javascript functions should be grouped into objects, javascript objects should be grouped into files, which can be considered the equivalent of java packages.

Use semicolon to end statements

I know it is optional, but it makes it easier for old C-hacks to parse as well as making it easier for the interpretors to spot errors if a typo is made.

Use the behaviour library

We are using the behaviourlibrary and all developers should become familiar with it. The key aspect of behaviour is that it allows us to separate javascript from markup. We should not write any scripts of functions within html, nor should we hard code onclick or onload attributes within html. With Ajax, much of our html will be generated and thus using markup is not an option.

Unique Id's, multiple classes

The ID of a DOM element must be unique, while multiple DOM elements can have the same class. A frequent mistake is to use a non unique id, for example on a cell in a table (if the table has multiple rows then a cell id is not unique). A class should be used if there are visual ramifications, otherwise normal attributes or external associative arrays should be used.

Understand this

The this automatic field in js works differently to java. If a member function of an object is called as a callback, then the this does not refer to the object but the calling scope. For example in the following code:

the _handle method is called when the mybutton is clicked, but the this will not refer to the myhandler object, but rather the DOM element of the button! The object var myhandler can be used instead of this in these circumstances.

Use 'single' instead of "double" quotes

Strings in js and html can be quoted with single or double quotes. We should use single quotes in the javascript and double quotes in markup. It is then simpler for js to quote double quotes when generating markup.

Use var keyword for variables inside for loop

I know this is supposed to be optional but IE does not seem to handle well for loops with variables that are not declared with var. Thus instead of writing:

we should write:

No labels

Contact the core Jetty developers at
www.webtide.com
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ...
scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery