<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>David Pratt &#187; namespacing</title> <atom:link href="http://daipratt.co.uk/tag/namespacing/feed/" rel="self" type="application/rss+xml" /><link>http://daipratt.co.uk</link> <description>Concerned about Website Construction &#38; SEO</description> <lastBuildDate>Wed, 25 Jan 2012 07:16:58 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.4</generator> <item><title>Defensive coding in JavaScript with Namespacing</title><link>http://daipratt.co.uk/defensive-coding-in-javascript-with-namespacing/</link> <comments>http://daipratt.co.uk/defensive-coding-in-javascript-with-namespacing/#comments</comments> <pubDate>Fri, 02 Oct 2009 10:05:42 +0000</pubDate> <dc:creator>daipratt</dc:creator> <category><![CDATA[Tech]]></category> <category><![CDATA[defensive]]></category> <category><![CDATA[javascript]]></category> <category><![CDATA[namespacing]]></category> <guid
isPermaLink="false">http://localhost/wordpress/?p=184</guid> <description><![CDATA[A tutorial on how to go about namespacing your JavaScript, plus a list of some of the benefits that you get from doing so.]]></description> <content:encoded><![CDATA[<p>Having learnt some harsh lessons early on in my career about the drawbacks of using generic/common function names, such as <i>init</i> or <i>basket</i> for instance &#8211; I once took out important &#8220;add to basket&#8221; functionality on a major retail website due to a conflicting function name that only manifested itself in production. Not good.</p><p>Since then, I have embraced JavaScripts namespacing ability (along with anonymous functions) to complement the defensive style that I quickly felt the need to adopt! I think that coding defensively is especially important because it is often impossible to conceive what the future business requirements of a site will be, what client side code could be thrown at it, or the caliber of developer working on it. The last thing you want is a developer from the marketing department innocuously dropping a “cool” image rotating script site wide in the document &lt;head&gt; and taking out business critical code as a result. It’s embarrassing for the guilty developer, but I think more so for the senior guy who should’ve coded more defensively in the first place.</p><p>This may or may not work for you, but I namespace everything to mitigate the chances of collision of functions, variables, objects, anything bar the root global variable name, from any new code that follows later, or more broadly <abbr
title="other peoples code">OPC</abbr>.</p><p>Anyway, a namespace essentially provides a container for all of your code to sit within.  At its simplest, it can be defined like so:</p><pre class="brush: javascript">
var myFirstNamespace = {};
</pre><p>This can then be extended in any manner you please. For example:</p><pre class="brush: javascript">
//Extend myFirstNamespace
myFirstNamespace.helloWorld= function() {
	console.log('Hello World!');
};
&nbsp;
//Use the myFirstNamespace properties
myFirstNamespace.helloWorld();
</pre><p>This would then write the following into your Fire Bug console:</p><pre class="brush: text">
Hello World!
</pre><p>Easy peasy.</p><p>As a real world example, at Bounty, everything new went into a <i>bounty</i> namespace, which in turn had things like <i>bounty.utils</i>, <i>bounty.ui</i> to separate the various functions that were held within.  Here&#8217;s a skeleton namespace that might make it easier to understand what I mean:</p><pre class="brush: javascript">
var bounty = {
	init : function(){
		//Initialise functions within the bounty namespace
		bounty.advert.fInit();
		bounty.ui.fInit();
		bounty.utils.fInit();
	},
	advert : {
		fInit : function(){
			//Initialise bounty.advert
		},
		fAdvertFunction1: function(){
			//An advert function
		},
		fAdvertFunction2: function(){
			//Another advert function
		}
	},
	ui : {
		fInit : function(){
			//Initialise bounty.ui
		},
		tabBox : {
			fInit : function(){
				//Initialise bounty.ui.tabBox
			}
		}
	},
	utils : {
		fInit : function(){
			//Initialise bounty.utils
		}
	}
};
$(document).ready(function(){
	//Initiliase bounty.
	bounty.init();
});
</pre><p>I find that this style of coding helps in enforcing a structure to the code, which is particularly helpful when there are large quantities of it, and when it comes to debugging.  Another positive side effect is that if your chosen code editor supports code folding, then you can quickly collapse everything and get a very clear architectural overview of what it does without having to trace your way through functions to understand what&#8217;s going on.</p> ]]></content:encoded> <wfw:commentRss>http://daipratt.co.uk/defensive-coding-in-javascript-with-namespacing/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>
