<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
  <id>tag:www.thejavascriptway.com,2005:/posts</id>
  <link type="text/html" href="http://www.thejavascriptway.com" rel="alternate" />
  
  <title>The JavaScript Way</title>
  <updated>2010-03-14T20:32:18Z</updated>
  <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/TheJavascriptWay" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="thejavascriptway" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
    <id>tag:www.thejavascriptway.com,2005:Post/9</id>
    <published>2010-03-14T20:32:18Z</published>
    <updated>2010-03-14T20:32:18Z</updated>
    <link type="text/html" href="http://www.thejavascriptway.com/posts/9-the-real-world-case-for-unobtrusive-javascript" rel="alternate" />
    <title>The real world case for unobtrusive JavaScript</title>
    <content type="html">&lt;div class="post published" id="post_9"&gt;
  
  

  &lt;p&gt;Unobtrusive JavaScript (&lt;span class="caps"&gt;UJS&lt;/span&gt;) has gained mainstream acceptance. Except for a few holdouts, the collective conscience of web developers has changed from thinking inline JavaScript is &lt;em&gt;good enough&lt;/em&gt; to accepting that &lt;span class="caps"&gt;UJS&lt;/span&gt; is the modern and &lt;em&gt;right&lt;/em&gt; way to add behaviour to web pages. But this is not the same thing as really &lt;em&gt;understanding&lt;/em&gt; how JavaScript relates to modern web development; I believe there&amp;#8217;s a &lt;a href="http://en.wikipedia.org/wiki/Cargo_cult"&gt;word&lt;/a&gt; for this kind of collective behaviour.&lt;/p&gt;
&lt;p&gt;I want to explain the real world, pragmatic and practical reasons for unobtrusive JavaScript. Actually, I think the word unobtrusive is misleading because unobtrusiveness is just a side effect of what I will call &lt;em&gt;structured&lt;/em&gt; JavaScript. It is not purity for purity&amp;#8217;s sake that should drive you towards a cleaner, more structured, more reusable and more scalable way to program JavaScript, but the benefits inherent in these qualities, including keeping your sanity when dealing with less-than-trivial functionality.&lt;/p&gt;
&lt;p&gt;First, let&amp;#8217;s examine the weaknesses of unstructured or obtrusive JavaScript:&lt;/p&gt;
&lt;ul class="definitions"&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s unstructured:&lt;/strong&gt;
This should be enough of a weakness in itself. Lack of structure results in messy code that increases the overhead of making changes to a point where it becomes nearly impossible.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It doesn&amp;#8217;t scale:&lt;/strong&gt;
Any sufficiently complex web application will implode from its own weight if you keep adding inline JavaScript. What seems simple to begin with will eventually evolve into a mess of inline event handlers, global functions and variables clashing with each other and strong coupling. It will become increasingly difficult to find anything at all and adding or changing functionality will eventually become a dreaded, near-impossible task where even the smallest change could bring the whole thing down.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s not testable:&lt;/strong&gt;
It&amp;#8217;s much, much harder to write automated tests for messy, coupled code than it is for properly structured code.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s not reusable:&lt;/strong&gt;
Yes, you may have some server-side function that generates the same code over and over again that you could call reusable, but that&amp;#8217;s like saying Java is conceptually simple because you have this huge &lt;span class="caps"&gt;IDE&lt;/span&gt; generating your huge classes, interfaces and &lt;span class="caps"&gt;XML&lt;/span&gt; configuration files for you. You&amp;#8217;re still littering your &lt;span class="caps"&gt;HTML&lt;/span&gt; with the exact same code over and over, increasing page load and complexity along the way. &lt;em&gt;You can&amp;#8217;t abstract away JavaScript&lt;/em&gt;.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It adds more coupling to your code:&lt;/strong&gt;
Inline code couples your JavaScript to something as volatile as &lt;span class="caps"&gt;HTML&lt;/span&gt;, resulting in easier breakage of semi-unrelated components of your application. You might argue that having it all in the same place makes it easier to see the coupling and change both at the same time, but well-structured unobtrusive JavaScript has considerably less coupling to begin with and can handle much more change to the &lt;span class="caps"&gt;HTML&lt;/span&gt; tree without breaking than inline code can ever dream of.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It forces responsibility overlap:&lt;/strong&gt;
By inlining JavaScript you force both designers and programmers (both server- and client-side) to be responsible for and understand the details of each other&amp;#8217;s work. This is not entirely avoidable; the content, style and structure of your page &lt;em&gt;will&lt;/em&gt; overlap, but by staying unobtrusive you can keep this to a much more sane level. A designer shouldn&amp;#8217;t have to journey deep inside the intestines of your inline JavaScript to change the way an element looks.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Your page elements can&amp;#8217;t have their own state:&lt;/strong&gt;
Unobtrusive JavaScript often ignores this as well, but when using inline code you&amp;#8217;re making sure your page elements (like a post, comment, etc) can&amp;#8217;t have their own internal state; they&amp;#8217;re not self-conscious. This is a basic requirement for good object-oriented programming which can make it much easier to deal with complexity.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The basic tenets of good programming apply to JavaScript just as much as they do to Ruby or Java programming. Modularity, proper structure, reusability and encapsulation provide exactly the same benefits for JavaScript programming as they do for other languages and &amp;#8220;obtrusive&amp;#8221; JavaScript breaks all of these practises. Inline JavaScript is to structured JavaScript what &lt;span class="caps"&gt;PHP&lt;/span&gt; is to Ruby on Rails: Its simplicity only works in its favour for very simple applications. JavaScript is a rebellious teenager in need of structure and discipline; you&amp;#8217;re the parent who must send it to boarding school.&lt;/p&gt;
&lt;p&gt;So what are these benefits I talk about? They&amp;#8217;re basically the opposite of the weaknesses of &lt;em&gt;not&lt;/em&gt; following best practises:&lt;/p&gt;
&lt;ul class="definitions"&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s structured:&lt;/strong&gt;
Again, this is a strength in itself. Structured code is a pre-requisite for most of the other best practises.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It scales:&lt;/strong&gt;
Once your code is properly modularised, decoupled and encapsulated, it becomes so much easier to add or change functionality. It&amp;#8217;s considerably less work than it would have been with an inline Frankensteinian mess of an application.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s less complex:&lt;/strong&gt;
Complex code is more difficult to understand, change and test.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s testable:&lt;/strong&gt;
You can test discrete components of your application&amp;#8217;s functionality without setting up scaffolding to support it or having unrelated tests break because your code is coupled.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s decoupled:&lt;/strong&gt;
You can change one part of the page&amp;#8217;s functionality without worrying that something completely unrelated will break.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It&amp;#8217;s reusable:&lt;/strong&gt;
Once your code is modular and decoupled it becomes much easier to reuse parts of it.
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Contrary to what a lot of people think, writing structured JavaScript is &lt;em&gt;not&lt;/em&gt; more work than using inline code. There is a small penalty at the very beginning where the overhead of separating behaviour from content is noticeable, but at a certain point the relation reverses to mean that inline code is &lt;em&gt;more&lt;/em&gt; work, and that threshold is very low.&lt;/p&gt;
&lt;p&gt;JavaScript is a real programming language. Stop using it like it&amp;#8217;s &lt;span class="caps"&gt;PHP&lt;/span&gt; and start using it to your own benefit.&lt;/p&gt;

  &lt;p class="meta"&gt;
    Posted by
    &lt;a href="http://www.thejavascriptway.com/" class="user"&gt;Tore Darell&lt;/a&gt;
    on
    &lt;span class="date"&gt;Mar 14 2010&lt;/span&gt;
          (&lt;a href="http://www.thejavascriptway.com/posts/9-the-real-world-case-for-unobtrusive-javascript#comments"&gt;0 comments&lt;/a&gt;)
      &lt;/p&gt;

  
&lt;/div&gt;</content>
    <author>
      <name>Tore Darell</name>
    </author>
  </entry>
  <entry>
    <id>tag:www.thejavascriptway.com,2005:Post/4</id>
    <published>2010-03-14T20:22:07Z</published>
    <updated>2010-03-14T20:22:07Z</updated>
    <link type="text/html" href="http://www.thejavascriptway.com/posts/4-15-javascript-development-no-nos" rel="alternate" />
    <title>15 JavaScript Development No-Nos</title>
    <content type="html">&lt;div class="post published" id="post_4"&gt;
  
  

  &lt;p&gt;A few weeks ago Chad Fowler posted a list of &lt;a href="http://chadfowler.com/2009/4/1/20-rails-development-no-no-s"&gt;20 Rails No-Nos&lt;/a&gt; which included 20 of the things people in the Rails community considered to be &amp;#8220;wrong&amp;#8221;. I thought it would be interesting to compile a similar list for JavaScript code and came up with 15 points I myself consider &amp;#8220;wrong&amp;#8221;. The list is of course in no way canonical, I&amp;#8217;m sure not everyone agrees with all the points or would like some others to be in there. I could only come up with 15, but I&amp;#8217;m sure others can come up with some additional points in the comments :)&lt;/p&gt;
&lt;h3&gt;1) Global variables&lt;/h3&gt;
&lt;p&gt;Like in most programming languages, using global variables in JavaScript is very much a no-no. They cause unpredictable behaviour and hard-to-track-down errors and have the potential to wreak havoc with not just your own code but other people&amp;#8217;s code too. There are several ways in which you can intentionally or accidentally create a global variable in JavaScript:&lt;/p&gt;
&lt;h4&gt;Creating a variable while in the global context&lt;/h4&gt;
&lt;p&gt;If you&amp;#8217;re not inside a function definition you&amp;#8217;re in the &lt;em&gt;global context&lt;/em&gt;, and creating a variable here will make it global.&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var foo = "global"; //Don't do this

function(){
  var bar = "local"; //This is ok
}();&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Not using the &lt;code&gt;var&lt;/code&gt; keyword&lt;/h4&gt;
&lt;p&gt;Any time you assign a variable without using the &lt;code&gt;var&lt;/code&gt; keyword, that variable becomes global:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;function(){
  foo = "global"; //Don't do this
  var bar = "local"; //This is ok
}();&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Assigning properties to the &lt;code&gt;window&lt;/code&gt; variable&lt;/h4&gt;
&lt;p&gt;Properties assigned to the global &lt;code&gt;window&lt;/code&gt; variable are the same as global variables:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;function(){
  window.foo = "global";//Don't do this
}();

function(){
  return foo; //Returns "global"
}();&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;Creating global functions&lt;/h4&gt;
&lt;p&gt;Named functions in JavaScript are function objects assigned to variables. This means that whenever you do something like this&amp;#8230;&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;function foo(){
  //...
};&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&amp;#8230;you&amp;#8217;re creating a global variable named &lt;code&gt;foo&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If this article were limited to only one point it would be to &lt;strong&gt;always use &lt;code&gt;var&lt;/code&gt;&lt;/strong&gt;. This is the one thing that&amp;#8217;s the easiest to remember and has the most significance when writing JavaScript code.&lt;/p&gt;
&lt;h3&gt;2) Inline JavaScript&lt;/h3&gt;
&lt;p&gt;Inline JavaScript is any JavaScript code that is mixed with &lt;span class="caps"&gt;HTML&lt;/span&gt;. There are two primary ways in which you can do this:&lt;/p&gt;
&lt;h4&gt;&lt;code&gt;script&lt;/code&gt; tags&lt;/h4&gt;
&lt;pre&gt;&lt;code class="html"&gt;&amp;lt;form id="foo"&amp;gt;
&amp;lt;!-- ... --&amp;gt;
&amp;lt;/form&amp;gt;
&amp;lt;script&amp;gt;
$('foo').observe('submit', function(e){
  if (!confirm("Are you sure?")) { e.stop(); }
});
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;&lt;code&gt;onclick&lt;/code&gt; and friends&lt;/h4&gt;
&lt;p&gt;All &lt;span class="caps"&gt;HTML&lt;/span&gt; elements support at least some events using &lt;code&gt;onfoo&lt;/code&gt; handler attributes, and additionally you can place javascript in a link element&amp;#8217;s &lt;code&gt;href&lt;/code&gt; tag:&lt;/p&gt;
&lt;pre&gt;&lt;code class="html"&gt;&amp;lt;form id="foo" onsubmit="if (confirm("Are you sure?")) { return true; } else { return false; }"&amp;gt;
&amp;lt;input type="text" value="Name" onclick="this.value='';" /&amp;gt;
&amp;lt;a href="javascript:$('foo').submit()"&amp;gt;Submit&amp;lt;/a&amp;gt;
&amp;lt;/form&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;All of the above examples will work fine, but are for several reasons outdated and very poor techniques. For one, it&amp;#8217;s just really messy; keeping &lt;span class="caps"&gt;HTML&lt;/span&gt; and JavaScript separate just makes everything a lot cleaner and easier to deal with. Secondly, they&amp;#8217;re not reusable.. A better way would be to mark elements using IDs and class names, this way you could write the JavaScript once and reuse it just by adding class names to your &lt;span class="caps"&gt;HTML&lt;/span&gt; elements.&lt;/p&gt;
&lt;h3&gt;3) &lt;code&gt;eval&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Also like most other programming languages, using &lt;code&gt;eval&lt;/code&gt; in JavaScript is considered.. well, evil. If you find yourself using eval, there&amp;#8217;s a big chance your approach is wrong. The one exception to this is parsing &lt;span class="caps"&gt;JSON&lt;/span&gt; data, which is easily done using eval, but it&amp;#8217;s much better to use a library for this, such as Douglas Crockford&amp;#8217;s &lt;a href="http://www.json.org/json2.js"&gt;json2.js&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;4) Old-style event handling&lt;/h3&gt;
&lt;p&gt;In the olden days, people would use two primary techniques to do event handling:&lt;/p&gt;
&lt;p&gt;The good old &lt;code&gt;onfoo&lt;/code&gt; &lt;span class="caps"&gt;HTML&lt;/span&gt; attributes:&lt;/p&gt;
&lt;pre&gt;&lt;code class="html"&gt;&amp;lt;a href="#" id="foo" onclick="handleFooClick();"&amp;gt;Click me&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And the slightly better JavaScript property functions using the same names:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var el = document.getElementById('foo');
el.onclick = function(){
  handleFooClick()
};&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Luckily things have improved since then and we can make use of modern browsers&amp;#8217; more advanced event handling. This means we can keep our event handlers separate from the &lt;span class="caps"&gt;HTML&lt;/span&gt; and we can attach several handlers to each element, prevent the default action from being performed and make use of event capture and bubbling. Internet Explorer, of course, does things its own way, but libraries such as Prototype and jQuery take care of that for us:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;//Prototype
el.observe('click', function(e){
  e.stop(); //Prevent the default action
  if (confirm("Are you sure?")) {
    doSomething();
  }
});&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;5) Using constructors for literal values&lt;/h3&gt;
&lt;p&gt;JavaScript has a few &amp;#8220;literal&amp;#8221; values that are shorthand for instantiating objects and adding properties to them. The most-often misused (or not-used) of these are the array and object literals. There&amp;#8217;s no need to use the &lt;code&gt;Array&lt;/code&gt; and &lt;code&gt;Object&lt;/code&gt; constructors:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var list = new Array();
list.push(1);
list.push(2);
list.push(3);

var map = new Object();
map["foo"] = 1;
map["bar"] = 2;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The above can be written more succinctly using literals:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var list = [1,2,3];
var map = {foo: 1, bar: 2};&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;6) &lt;code&gt;alert("In your face")&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Most of the time, using the &lt;code&gt;alert&lt;/code&gt; method is unnecessary. The most prevalent misuse of this method is for debugging purposes, but using tools such as &lt;a href="http://getfirebug.com/"&gt;Firebug&lt;/a&gt; and equivalents for other browsers is a much better and more efficient way to debug JavaScript code.&lt;/p&gt;
&lt;h3&gt;7) &lt;code&gt;element.style&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;You can change an element&amp;#8217;s style directly by changing properties on its &lt;code&gt;style&lt;/code&gt; property:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;el.style.backgroundColor = "#ff0000";
el.style.color = "#00ff00";&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is usually not a good idea because you want to keep the styling separate from the behaviour and changing an element&amp;#8217;s style directly will override anything you&amp;#8217;ve defined in your &lt;span class="caps"&gt;CSS&lt;/span&gt;. A much better way to do this is to assign class names to elements and leave the styling up to the stylesheet:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var el = $$('.post').first();
el.addClassName("marked");&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code class="css"&gt;.post.marked {
  background-color: #ff0000;
  color: #00ff00;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way your JavaScript only has to decide if an element should be in a &amp;#8220;marked&amp;#8221; state or not, and the &lt;span class="caps"&gt;CSS&lt;/span&gt; can decide what that means in terms of styling. When you come back to this code a month later because the definition of &amp;#8220;marked&amp;#8221; has changed, you (or someone else!) will find what they&amp;#8217;re looking for in the most logical place, which is in the &lt;span class="caps"&gt;CSS&lt;/span&gt; stylesheet and not hidden deep inside some JavaScript code. If your new colleague gets tasked with changing this, he&amp;#8217;s likely to use Firebug to see what exactly happens when a post gets marked. He will then see that the &amp;#8220;marked&amp;#8221; class name gets added, and Firebug will additionally tell him exactly in which &lt;span class="caps"&gt;CSS&lt;/span&gt; file and on which line this rule is defined.&lt;/p&gt;
&lt;h3&gt;8) No namespacing&lt;/h3&gt;
&lt;p&gt;This is similar to the &amp;#8220;no globals&amp;#8221; rule because the easiest way to avoid polluting the global namespace is to create your own. A site will usually create one global variable which serves as its namespace:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var MySite = {};
MySite.Foo = Class.create(/* ... */);&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This way the site will have all of its &amp;#8220;stuff&amp;#8221; in one place, and the only global variable created was the &lt;code&gt;MySite&lt;/code&gt; variable. If you&amp;#8217;re creating a class that&amp;#8217;s of general use, it&amp;#8217;s somewhat permissible to place this in the global scope. The Prototype library, for instance, does this with classes such as &lt;code&gt;Hash&lt;/code&gt; and &lt;code&gt;Enumerable&lt;/code&gt;, while other libraries such as &lt;span class="caps"&gt;YUI&lt;/span&gt; create only one global variable which contains everything in it.&lt;/p&gt;
&lt;h3&gt;9) &lt;code&gt;window.open&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Except for a few cases, this method should really not be used. The most egregious misuse of this method is to open an entire website in a new window with no address bar, toolbar or scrollbars.&lt;/p&gt;
&lt;h3&gt;10) &lt;code&gt;window.location&lt;/code&gt;, &lt;code&gt;document.href&lt;/code&gt;, &lt;code&gt;window.status&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;These, and I&amp;#8217;m sure many other, properties are often misused. The &lt;code&gt;location&lt;/code&gt; and &lt;code&gt;document.href&lt;/code&gt; properties are most often used to redirect the user to a different page. While this is not really ideal in most cases, there are some situations where it can be useful, but they should be used sparingly. The &lt;code&gt;window.status&lt;/code&gt; property is just wrong, and I&amp;#8217;m not even sure if it works in modern browsers, but if you see this being used in a script you can be sure the script is outdated.&lt;/p&gt;
&lt;h3&gt;11) &lt;code&gt;document.forms&lt;/code&gt;, &lt;code&gt;document.all&lt;/code&gt;, &lt;code&gt;document.layers&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Like &lt;code&gt;window.status&lt;/code&gt;, these properties are relics from the past which should not be used today. They were used as shortcuts to get to various parts of the document, but there are much better ways to do that nowadays. To get to an element in the &lt;span class="caps"&gt;DOM&lt;/span&gt; today, you should use the &lt;code&gt;getElementById&lt;/code&gt; and &lt;span class="caps"&gt;DOM&lt;/span&gt; access methods and properties on elements such as &lt;code&gt;element.childElements&lt;/code&gt; and &lt;code&gt;element.parentNode&lt;/code&gt;. The most recent browsers even lets you access arbitrary elements using XPath or &lt;span class="caps"&gt;CSS&lt;/span&gt;. If you&amp;#8217;re using a library such as Prototype or jQuery, these include some very intuitive and easy to use methods for walking the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree.&lt;/p&gt;
&lt;h3&gt;12) &lt;code&gt;setTimeout&lt;/code&gt; and &lt;code&gt;setInterval&lt;/code&gt; using strings&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;setTimeout&lt;/code&gt; and &lt;code&gt;setInterval&lt;/code&gt; functions allow you to execute a piece of code at a later point or at a set interval. They can take either a string which is evaluated as code or a function which is run directly. The preferred option is to use a function.&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;//This is wrong
setTimeout("element.addClassName('foo');", 100);

//This is right
setTimeout(function(){
  element.addClassName("foo");
}, 100);&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;13) &lt;code&gt;with&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;The &lt;code&gt;with&lt;/code&gt; keyword allow you to execute a piece of code as if the global context was an arbitrary object:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var element = $('foo');
with (element) {
  title = "foo"; //element.title = "foo"
  getChildren().first().remove(); //element.getChildren.first().remove()
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While this may save a few keystrokes it can cause more trouble than it&amp;#8217;s worth. The most harmful side effect of using the &lt;code&gt;with&lt;/code&gt; statement is the accidental creation of global variables, which we already know are evil. If you try to assign a property that the object (&lt;code&gt;element&lt;/code&gt; from the example) doesn&amp;#8217;t already have, this will evaluate to mean a variable name, which without the &lt;code&gt;var&lt;/code&gt; keyword will be considered global.&lt;/p&gt;
&lt;h3&gt;14) &lt;code&gt;document.write&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;Like &lt;code&gt;window.open&lt;/code&gt;, this method is a relic from the web&amp;#8217;s early days and should not be used. If you see this in a script, it&amp;#8217;s likely that it&amp;#8217;s a few years past its expiration date.&lt;/p&gt;
&lt;h3&gt;15) Not following naming conventions&lt;/h3&gt;
&lt;p&gt;The naming convention for variables and properties in JavaScript is as follows:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Local variable: &lt;code&gt;mixedCase&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;Object property: &lt;code&gt;object.mixedCase&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;Instance method: &lt;code&gt;object.mixedCase()&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;Class property: &lt;code&gt;Class.mixedCase&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;Class method: &lt;code&gt;Class.mixedCase()&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;Class name: &lt;code&gt;CamelCase&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;Namespace object: &lt;code&gt;CamelCase&lt;/code&gt;, or &lt;code&gt;CAPS&lt;/code&gt; in some cases&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While not following these conventions is not going to break your script, it&amp;#8217;s going to make it harder for others to read and understand. In particular, constructor functions (classes) should use an uppercase first letter to distinguish it from other functions. This is also the convention if your classes are properties of a namespace object, as they should be most of the time: &lt;code&gt;new MySite.ClassName()&lt;/code&gt;.&lt;/p&gt;

  &lt;p class="meta"&gt;
    Posted by
    &lt;a href="http://www.thejavascriptway.com/" class="user"&gt;Tore Darell&lt;/a&gt;
    on
    &lt;span class="date"&gt;Mar 14 2010&lt;/span&gt;
          (&lt;a href="http://www.thejavascriptway.com/posts/4-15-javascript-development-no-nos#comments"&gt;0 comments&lt;/a&gt;)
      &lt;/p&gt;

  
&lt;/div&gt;</content>
    <author>
      <name>Tore Darell</name>
    </author>
  </entry>
  <entry>
    <id>tag:www.thejavascriptway.com,2005:Post/3</id>
    <published>2009-03-26T16:31:30Z</published>
    <updated>2009-04-02T22:38:36Z</updated>
    <link type="text/html" href="http://www.thejavascriptway.com/posts/3-namespacing-your-code-in-an-object" rel="alternate" />
    <title>Namespacing your code in an object</title>
    <content type="html">&lt;div class="post published" id="post_3"&gt;
  
  &lt;div class="problem" id="problem_1"&gt;
  &lt;p class="user"&gt;
    &lt;a href="http://www.thejavascriptway.com/"&gt;August Lilleaas&lt;/a&gt; says:
  &lt;/p&gt;

      &lt;h3&gt;Namespacing your code in an object&lt;/h3&gt;
  
  &lt;div class="content"&gt;
    &lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; This is the (and my) first post/question, and it&amp;#8217;s more of a suggestion or finished article than a question or problem that people can comment on. Now, on with the show!&lt;/p&gt;
&lt;p&gt;You can use a shotgun blast of local functions.&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;var showCalendar = function(date){
	// ...
}

var daysInMonth = function(){
	// ...
}

var firstDayOfMonth = function(){
	// ...
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can also tuck your code in an object. This cleans up things quite a bit.&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;Calendar = {
	show: function(date){
		// ...
	},
	
	daysInMonth: function(){
		// ...
	},
	
	firstDayOfMonth: function(){
		// ...
	}
}

Calendar.show(new Date())&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In addition to providing a clean namespace, an object also gives you &lt;code&gt;this&lt;/code&gt;, which is very useful. You can use that to refer to variables and set states from inside the object.&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;Calendar = {
	init: function(date, argetDomId){
		this.date = new Date()
		this.target = document.getElementById(targetDomId || "default")
		this.width = 200
		this.height = 500
	},
	
	render: function(){
		// use this.width, this.target etc here
	}
	
	setDate: function(newDate){
		this.date = newDate
	},
	
	firstDayOfMonth: function(){
		// ...
	}
}

Calendar.init(new Date())
Calendar.init(new Date(2007, 1, 24), "myCalendarTarget")&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;

&lt;/div&gt;

  &lt;p&gt;In this first submission, August presents not just one but two essential patterns that should be used in JavaScript development:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Namespacing:&lt;/strong&gt; One of the big &lt;em&gt;dont&amp;#8217;s&lt;/em&gt; in JavaScript development is &amp;#8220;don&amp;#8217;t pollute the global namespace&amp;#8221;. I.e. create as few global variables as you possibly can. The way to achieve this is simple: Create &lt;em&gt;one&lt;/em&gt; global object which serves as a namespace for all your other objects. Namespacing is important in most programming languages, but in the unpredictable environment in which JavaScript runs, it becomes essential to keep interference to a minimum, because you simply don&amp;#8217;t know what other code might be in use on the same page.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Encapsulation:&lt;/strong&gt; JavaScript is an object-oriented language, and we should take advantage of that. Objects have internal state, which means you can put everything related into a black box which knows how to deal with its internals. Outside code only interacts with the object through its public interface, thus reducing coupling.&lt;/p&gt;

  &lt;p class="meta"&gt;
    Posted by
    &lt;a href="http://www.thejavascriptway.com/" class="user"&gt;Tore Darell&lt;/a&gt;
    on
    &lt;span class="date"&gt;Mar 26 2009&lt;/span&gt;
          (&lt;a href="http://www.thejavascriptway.com/posts/3-namespacing-your-code-in-an-object#comments"&gt;0 comments&lt;/a&gt;)
      &lt;/p&gt;

  
&lt;/div&gt;</content>
    <author>
      <name>Tore Darell</name>
    </author>
  </entry>
  <entry>
    <id>tag:www.thejavascriptway.com,2005:Post/2</id>
    <published>2009-03-25T13:08:11Z</published>
    <updated>2009-04-02T22:38:43Z</updated>
    <link type="text/html" href="http://www.thejavascriptway.com/posts/2-welcome-to-the-javascript-way" rel="alternate" />
    <title>Welcome to The JavaScript Way</title>
    <content type="html">&lt;div class="post published" id="post_2"&gt;
  
  

  &lt;p&gt;As you can probably tell, it&amp;#8217;s directly inspired by &lt;a href="http://www.therailsway.com/"&gt;The Rails Way&lt;/a&gt; which has been doing a great job of sharing best practises and ideas in the Rails world. We want to extend that to the very closely related JavaScript world because we think there are lots of brilliant ideas out there that should be shared among a wider audience. The &lt;em&gt;building blocks&lt;/em&gt; of scalable, structured, modular and efficient JavaScript and Ajax applications are many, but so far it seems they&amp;#8217;re not widely used, and we aim to change that!&lt;/p&gt;
&lt;p&gt;If you have a question/problem/anything you want to ask about or share, just log in with your OpenID to the right and you&amp;#8217;ll be able to submit it!&lt;/p&gt;

  &lt;p class="meta"&gt;
    Posted by
    &lt;a href="http://www.thejavascriptway.com/" class="user"&gt;Tore Darell&lt;/a&gt;
    on
    &lt;span class="date"&gt;Mar 25 2009&lt;/span&gt;
          (&lt;a href="http://www.thejavascriptway.com/posts/2-welcome-to-the-javascript-way#comments"&gt;2 comments&lt;/a&gt;)
      &lt;/p&gt;

  
&lt;/div&gt;</content>
    <author>
      <name>Tore Darell</name>
    </author>
  </entry>
</feed>

