Standardize style makes code easy to work with and maintain especially in larger organizations. See the
by the Dojo folks.
objects, and allows your scripts to be loaded and initialized dynamically.
When writing components use objects and object hierarchies to organize related
objects and prevent naming collision.
- Use the prototype property to define shared behavior and to extend objects
Use the prototype property to extend, add functionality, or provide defaults to your objects. Use the prototype property to define shared functions (behavior)
state that span pages on the server
is pressed, if the browser is relaunched/crashes, or if you navigate to another page.
Store state that spans pages on the server as Java objects scoped to the
The client and server objects should be synched on page refreshes and page loads.
component unless absolutely necessary. Consider parameterizing data in your functions instead of hard coding the data.
- Use object literals as flexible function parameters
Object literals are very handy in that they can be used as generic for passing in arguments to a function.
Since the function signature does not change if you choose to require more properties in a function, consider using an object literal as the parameter for a function.
Not to be confused with a zip compression
scheme compression refers to removing the white spaces and shortening the names of variables and functions
- Do not compress your Jar files that contain your scripts
Do not use zip compression on jar files with scripts or styles that are served by a server side component. Compressing those jars
will cause the server to decompress the file for each client request for the script or resource. Larger scripts could cause performance problems for your server.
- Protect your server side assets
regardless of whether the request originated from an AJAX client or not.
- Consider using JSON for model data transport
While the XML is still a
valid format for model data transport in AJAX (especially in cases where you're communicating with XML based services or your
services must also address non-AJAX based clients), you should consider using JSON to communicate
- Use caution with
If you choose to use
generates minimal HTML. Rely instead on CSS for enhancing the presentation. Remember
to always strive to separate content from presentation. Make sure to de-register event listeners
in the existing
innerHTMLof the element before re-setting the
as it will lead to memory leaks. Keep in mind that instances of DOM elements inside the
element.innerHTMLare lost when you replace
the content and references to those elements will be lost as well.
- De-reference unused objects
Make sure you de-reference variables when they are no longer needed so they may be garbage collected.
If you are using
element.innerHTML, make sure you de-reference any listeners in the code before it is replaced.
- Use care with closures
If you use closures take caution not to hold on to references to browser objects such as DOM related objects using the local variables of a closure as this can lead to memory leak. Objects are not garbage collected until all references to objects are gone. Make sure you understand closures in order to avoid creating them accidentally.
- Enable resource overrides in your components
Provide a means to override the loading of resources such as dependent scripts, CSS files, or
jar your component for distribution with a JSP tag or JSF component, move those resource files into the
WEB-INF directory and create code to stream them out to the client. This will
without rebuilding and re-packaging the component.
- Design with I18n in mind
perspective you need to do two things: Set the page encoding and make sure localized text is passed back to your
server backend. Consider using XML as the return content type for localized data as XML can specify encoding
information that is understood and applied by the XMLHttpRequest object. If you are passing around
localized content you need to make sure you server encodes the return content properly.
What would you add to the list?