Safer JavaScript through Encapsulation

Not everyone writes 100% of their JavaScript. With sites like The JavaScript Source and JavaScript Kit it’s easy to just copy and paste a bit of free code to quickly get some desired functionality on your site. The trouble is, because there are about as many authors as there are scripts in those libraries, you will inevitably start running into trouble with conflicting variable and function names as you incorporate more and more scripts.

Let’s say for example, that I want to run this roman numeral conversion script in the footer of my site so that the copyright year is automatically converted into roman numerals. Then for fun, I want to include this random number game on one of my pages. Unfortunately this scenario poses a problem. Both scripts use a global function called Init().

The traditional way of getting around this problem was renaming the functions to something more specific. So the Init()s would become InitRoman() and InitRand() respectively for both scripts. But not only is that annoying to have to do–especially if it isn’t your own code–but it’s also very messy.

Luckily there is a cleaner, better way around the problem. It’s done by using object oriented programming principles. Though JavaScript isn’t a fully fledged OO language, it does support similar behaviour, such as encapsulation. Encapsulation ensures good code modularity, which keeps routines separate and less prone to conflict with each other.

With that in mind, let’s take a look at how we can fix the conflicting Init() function problem. Rather than doing this:

function InitRoman(){
	//roman numeral initialization code here
}

function InitRand(){
	//random number game initialization code here
}

InitRoman();
InitRand();

we can do this:

var Roman = {
	Init : function(){
		//roman numeral initialization code here
	}
}

var Rand = {
	Init : function(){
		//random number game initialization code here
	}
}

Roman.Init();
Rand.Init();

Now isn’t that a lot nicer to look at and work with? With encapsulation you get to keep your Init() function name for both scripts without any conflicts.

The only thing is, of course, that if you’re copying and pasting scripts from all over the ‘net you’re kind of at the mercy of the authors of those scripts and will be waiting a long time for them to apply encapsulation to their existing code.

Tags:


redtube downloader redtube video download redtube video download redtube videos download redtube xvideos xvideos downloader xvideos video download xvideos video download xvideos videos download xvideos metacafe metacafe downloader metacafe video download metacafe video download metacafe videos download metacafe xhamster xhamster downloader xhamster video download xhamster video download xhamster videos download xhamster daylimotion daylimotion downloader daylimotion video download daylimotion video download daylimotion videos download daylimotion facebook facebook downloader facebook video download facebook video download facebook videos download facebook badjojo badjojo downloader badjojo video download badjojo video download badjojo videos download badjojo dachix dachix downloader dachix video download dachix video download dachix videos download dachix keezmovies keezmovies downloader keezmovies video download keezmovies video download keezmovies videos download keezmovies shufuni shufuni downloader shufuni video download shufuni video download shufuni videos download shufuni vimeo vimeo downloader vimeo video download vimeo video download vimeo videos download vimeo veoh veoh downloader veoh video download veoh video download veoh videos download veoh theync theync downloader theync video download theync video download theync videos download theync ehow ehow downloader ehow video download ehow video download ehow videos download ehow xtube xtube downloader xtube video download xtube video download xtube videos download xtube stupidvideos stupidvideos downloader stupidvideos video download stupidvideos video download stupidvideos videos download stupidvideos AccessHollywood AccessHollywood downloader AccessHollywood video download AccessHollywood video download AccessHollywood videos download AccessHollywood ClipJunkie ClipJunkie downloader ClipJunkie video download ClipJunkie video download ClipJunkie videos download ClipJunkie Ebaumsworld Ebaumsworld downloader Ebaumsworld video download Ebaumsworld video download Ebaumsworld videos download Ebaumsworld FunnyOrDie FunnyOrDie downloader FunnyOrDie video download FunnyOrDie video download FunnyOrDie videos download FunnyOrDie FootyTube FootyTube downloader FootyTube video download FootyTube video download FootyTube videos download FootyTube pornhub pornhub downloader pornhub video download pornhub video download pornhub videos download pornhub