-
Notifications
You must be signed in to change notification settings - Fork 1
JavaScript Guidelines
Benjamin Oakes edited this page Nov 12, 2012
·
3 revisions
Just some general guidelines for things worth knowing about JavaScript.
- Know that guidelines are just that: guidelines. Know when to ignore the guide.
- Know that JavaScript is its own langauge with its own community.
- Know JavaScript, including the basic parts: objects, functions, and prototypes.
- If you want to learn more, consider reading JavaScript the Good Parts or Eloquent Javascript.
- A simple test: without looking up anything what does the following JavaScript do?
// NOTE: Don't modify built-in prototypes in production code. Big Brother is watching.
Number.prototype.hours = function () { return this * 60 * 60; };
(function (n) { console.log(n + 'hrs = ' + n.hours() + 's'); }(2));- Target ECMAScript 5 ("ES5") when possible.
- Listen to what JSHint tells you.
- Listen to what JSLint tells you too, and consider using JSLint's style.
Nearly everybody is convinced that every style but their own is ugly and unreadable. Leave out the "but their own" and they're probably right...
-- Jerry Coffin (on indentation)
- Name things the JavaScript way (
fooBar = 1instead offoo_bar = 1orFooBar = 1). - Know where JavaScript stops and where the browser API begins.
- Know how JavaScript is used outside the browser (e.g. Node.js, Rhino), and how that can influence your code.
- Avoid globals when possible, e.g. via the module pattern. Rule of thumb: if it can be private, maybe it should be.
- Avoid inline JavaScript. (We could use a
Content-Security-Policyto enforce this in compliant browsers.) - Leverage existing libraries when possible.
- Examples: jQuery, Backbone, Underscore, Mustache, Handlebars, Moment, Jasmine, Timecop, etc.
- Avoid lots of nested callbacks. Libraries (such as Backbone and async) and patterns are a great help.
Public Wiki by hedgeyedev is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
