This link has been bookmarked by 25 people . It was first bookmarked on 27 Dec 2006, by Adam Skinner.
-
Lourens KeyserJavascript examples
-
LiteIntroduction
This document is a list of best practices and preferred ways of developing javascript code, based on opinions and experience from many developers in the javascript community. Since this is a list of recommendations rather than a list of absolute rules, experienced developers may have slightly differing opinions from those expressed below.
Table Of Contents
1. Always Use 'var'
2. Feature-Detect Rather Than Browser-Detect
3. Use Square Bracket Notation
4. Avoid 'eval'
5. Reference Forms and Form Elements Correctly
6. Avoid 'with' Statements
7. Use onclick In Anchors Instead Of javascript: Pseudo-Protocol
8. Use The Unary + Operator To TypeConvert To Number
9. Avoid document.all
10. Don't Use HTML Comments In Script Blocks
11. Avoid Cluttering The Global Namespace
12. Avoid sync "Ajax" calls
13. Use JSON
14. Use Correct <script> Tags
Always Use 'var'
Variables in javascript either have global scope or function scope, and using the 'var' keyword is vital to keeping them straight. When declaring a variable for use either as a global variable or as a function-level variable, always prefix the declaration with the 'var' keyword. The example below highlights the potential problem caused by not doing so.
Problem Caused By Not Using Var
var i=0; // This is good - creates a global variable
function test() {
for (i=0; i<10; i++) {
alert("Hello World!");
}
}
test();
alert(i); // The global variable i is now 10!
Since the variable i inside the function was not declared as a function-level variable by using the 'var' keyword, it references the global variable in this example. It is a good idea to always declare global variables using 'var', but it is vital to declare function-scoped variables using 'var'. The two approaches below are functionally identical.
Fixed Function
function test() {
var i=0;
for (i=0; i<10; i++) {
alert("Hello World!");
}
}
Fixed Function
function test() {
for (var i=0; i<10; i++) {
alert("Hello World!");
} -
-
if it returns true, then the HREF of the anchor will be followed like a normal link. If it returns false, then the HREF will be ignored. This is why "return false;"
-
-
vivelafranceJavascript reference and best practices
-
Gavin AllanThis document is a list of best practices and preferred ways of developing javascript code, based on opinions and experience from many developers in the javascript community.
-
-
With dot notation, the property name is hard-coded and cannot be changed at run-time. With bracket notation, the property name is a string which is evaluated to resolve the property name. The string can be hard-coded, or a variable, or even a function call which returns a string property name. -
Prefixing a variable or expression with + will force it to evaluate as a number, which can then be successfully used in a math operation.
- 1 more annotations...
-
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.