GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.crockford.com — Code Conventions for the JavaScript Programming Language BlogBooksVideos2026 AppearanceSlidesJavaScriptMistyJSLintJSONProntoGithubElectric CommunitiesFlickr Photo AlbumBlue SkyMastodon/Layer8ResearchGatePronouns: pe/perApteraThe best is yet to comeAboutCode Conventions for the JavaScript Programming LanguageThis is a set of coding conventions and rules for use in JavaScript programming. The long-term value of software to an organization is in direct proportion to the quality of the codebase. Over its lifetime, a program will be handled by many pairs of hands and eyes. If a program is able to clearly communicate its structure and characteristics, it is less likely that it will break when modified in the never-too-distant future. Code conventions can help in reducing the brittleness of programs. All of our JavaScript code is sent directly to the public. It should always be of publication quality. Neatness counts. Diversity of people, culture, and perspective is a good and valueable thing. Diversity of programming styles is a bad thing. It creates friction, impeding the ability of people to work together. It can make it easier for bugs to form and hide. JavaScript Files JavaScript programs should be stored in and delivered as JavaScript code should not be embedded in HTML files unless the code is specific to a single session. Code in HTML adds significantly to pageweight with no opportunity for mitigation by caching, minification, and compression. WhitespaceWhere possible, these rules are consistent with centuries of good practice with literary style. Deviations from literary style should only be tolerated if there is strong evidence of a significant benefit. Personal preference is not a significant benefit. Blank lines improve readability by setting off sections of code that are logically related. Blank spaces should always be used in the following circumstances:
while (true) {
Every statement should begin aligned with the current indentation. The outermost
level is at the left margin. The indentation
increases by 4 spaces when the last token on the previous line is
The ternary operator can be visually confusing, so wrap the entire ternary expression in parens. The condition, the
let integer = function (
value,
default_value
) {
value = resolve(value);
return (
typeof value === "number"
? Math.floor(value)
: (
typeof value === "string"
? value.charCodeAt(0)
: default_value
)
);
};
Clauses ( Use of tabs invites confusion, argument,and crying, with little compensating value. Do not use tabs. Use space. CommentsBe generous with comments. It is useful to leave information that will be read at a later time by people (possibly your future self) who will need to understand what you have done and why. The most valuable comments talk about intentions and reasons, things that are not easily discovered by reading the code. The comments should be well-written and clear, just like the code they are annotating. An occasional nugget of humor might be appreciated. Frustrations and resentments will not. It is important that comments be kept up-to-date. Erroneous comments can make programs even harder to read and understand. Make comments meaningful. Focus on what is not immediately visible. Don't waste the reader's time with stuff like // Set i to zero.
i = 0;
Use line comments, not block comments. The comments should start at the left margin. Variable DeclarationsAll variables should be declared before used. JavaScript does not require this, but doing so makes the program easier to read and makes it easier to detect undeclared variables that may become implied. Implied global variables should never be used. Use of global variables should be minimized. let current_entry; // currently selected table entry
let level; // indentation level
let size; // size of the table
Function DeclarationsAll functions should be declared before they are used. Inner functions should come after the outer function's variable declarations. This helps make it clear what variables are included in its scope. There should be no space between the name of a function and the
function outer(c, d) {
let e = c * d;
function inner(a, b) {
return (e * a) + b;
}
return inner(0, 1);
}
This convention works well with JavaScript because in JavaScript, functions and object literals can be placed anywhere that an expression is allowed. It provides the best readability with inline functions and complex structures. function getElementsByClassName(className) {
let results = [];
walkTheDOM(
document.body,
function (node) {
let array; // array of class names
let ncn = node.className; // the node's classname
// If the node has a class name, then split it into a list of simple names.
// If any of them match the requested name, then append the node to the list of results.
if (ncn && ncn.split(" ").indexOf(className) >= 0) {
results.push(node);
}
}
);
return results;
}
If a function literal is anonymous, there should be one space between
the word div.onclick = function (e) {
return false;
};
that = {
method: function () {
return this.datum;
},
datum: 0
};
Use of global functions should be minimized. When a function is to be invoked immediately, the entire invocation expression should be wrapped in parens so that it is clear that the value being produced is the result of the function and not the function itself. let collection = (function () {
let keys = [];
let values = [];
return {
get: function (key) {
let at = keys.indexOf(key);
if (at >= 0) {
return values[at];
}
},
set: function (key, value) {
let at = keys.indexOf(key);
if (at < 0) {
at = keys.length;
}
keys[at] = key;
values[at] = value;
},
remove: function (key) {
let at = keys.indexOf(key);
if (at >= 0) {
keys.splice(at, 1);
values.splice(at, 1);
}
}
};
}());
Names Names should be formed from the 26 upper and lower case letters ( Do not use Most variables and functions should start with a lower case letter. Constructor functions that must be used with the Global variables should be avoided, but when used should be in StatementsSimple Statements Each line should contain at most one statement. Put a JavaScript allows any expression to be used as a statement. This can mask some errors, particularly in the presence of semicolon insertion. The only expressions that should be used as statements are assignments, invocations, and Compound Statements Compound statements are statements that contain lists of statements enclosed in
Labels Statement labels should be avoided. Only these statements should be labeled:
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |