User:Jesdisciple/Manual of Style

This is a list of things to avoid in the JavaScript book.
 * Implicitly global variables.
 * Wrong:

function foo{ bar = 1 }
 * Right:

function foo{ var bar = 1 // Or if you really want the global one: window.bar = 1 }
 * Inline event handlers.
 * Wrong:

&lt;html&gt; &lt;body&gt; &lt;div onclick="alert('*headdesk*');"&gt; This is wrong! &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;
 * Right:

&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt; window.onload = function { document.getElementById('foo').onclick = function { alert('The Force is strong with this one.'); };           };        &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div id="foo"&gt; This is right! &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;
 * Looping through an array with for..in. Arrays only have anything special about them for numeric indices.  Use an object if not all of your indices are numeric.  Array.prototype was made to be modified, but the type was not made to be associative.