Code Conventions: JavaScript

Organizing

There can be six major parts:

1. Passing Variable Register: This is to register any variable across the HTML files. Do not use crossing file variable directly in body of JavaScript, instead declare here. For instance:

; var localVar = parent.superVar

2. Passing Function Register: To register the functions calling other functions located in different HTML file. Do not call crossing file function directly in body of JavaScript, instead declare here. For instance:

; function closeProcess()
{
; parent.closing()
}

Put Passing Variable Register and Passing Function Register at top of JavaScript file to enable easier updating during coding phrase.

3. Local Global Variable Declaration.

4. Event Functions: Include all event functions here, such as onload, etc.

5. HTML Action Functions: If the function is called by HTML file, initialized by the user, it should be included here.

6. Internally Called Functions: Functions called by event functions or action functions.

End of Statement

JavaScript allows the developer to decide whether or not to end a line with a semicolon. If the semicolon is not provided, JavaScript considers the end of the line as the end of the statement. It is suggested to use semicolon instead of nature line break. Furthermore, you can indeed use semicolon in front of each statement, not only emphasis the line break, but also to increase visual impact on the indentation. All examples in this post are in this format.

Comments

It is suggested to use closed comments tag as much as possible: <!-- -->. By doing this, it would be relatively easier to keep coding parts clearer. In addition, if you are using PHP Wrapping JavaScript Debugging Method, as introduced here:
http://koncordpartners.blogspot.com/2010/04/how-to-enforce-download-of-javascript.html, it is essential to use the closed comments.

DHTML

Use + to concatenate every line of HTML codes, such as:

; var htmlBody = ''
+ ' <table> ...
+ ' <tr> ...

Whitespace within string serves no purpose for HTML. However, it can make visual alignment for coding purpose.

Function Declaration

Always use most orthdox variablized function declaration:

; funcation func()
{
...
}

Avoid to use variablized function declaration like var func = function() {...}, unless it is necessary. If you do use variablized function declaration, make sure it had been placed before the caller. For details please see http://koncordpartners.blogspot.com/2010/04/function-declaration-in-javascript.html.

Form With PHP Behind

Use '' instead of null. Accordingly, the PHP end should be:

if (0!=strlen(trim(@$_POST["theInputItem"]))) {...};

When use function to assign value to form, string parameter would need to be assigned with '' in consideration of it might be null or undefined:

; document.theForm.theInputItem.value = (parameter) ? parameter : ''

For details, please refer to http://koncordpartners.blogspot.com/2010/04/trouble-with-internet-explorer.html.


http://javascript.crockford.com/code.html

No comments:

Post a Comment

Labels