Always Namespace Variable Names in JavaScript

After running into a few issues with variable naming collisions over the past few days, it drives home how much we all should be namespacing our variable names in JavaScript. When writing JavaScript code that is only in use on your own website, you should still always namespace your variables. If you are writting a JavaScript library that will be in use on any website a user puts it on, namespacing your variable names is a minimum requirement.

In addition, if you are writing a library that extends built-in objects, such as the way Prototype JS does with the Object object, it is extremely important that you namespace the methods and properties you add to the built-in objects. Prototype JS adds a .bind() method to the Object object, which is not a problem until you also include jQuery on the page, which also has a jQuery().bind() method. However, now that jQuery recommends that you use jQuery().on() instead, it is not as much of an issue, but if you have legacy jQuery sticking around, it can cause some interesting performance issues that are hard to pin down.

In order to namespace variable and method names in JavaScript, there are a couple of ways to implement it.

  1. Create a separate object, and make all methods and properties as methods and properties on the separate object. This can be done as below where tgsn is the namespaced variable object.
tgsn = {
		property1: 123, 
    	method2:function() {
    		alert("method");
        }
}
  1. Add a prefix to the name of every property and method that you use.
var tgsnProperty = 123,
	tgsnMethod = function() {
		alert("method");
};

Either of these two methods will work whether it is a stand-alone object or are extending built-in objects.

comments powered by Disqus

Related Posts

JavaScript Templating

Many times it becomes useful to be able to make an AJAX request for some data, insert it into some HTML that is already on the client, and then display it to the user. There are a few ways to implement this, each approach has its benefits and drawbacks. String Concatenation Possibly the simplest way to accomplish the templating in JavaScript is to use simple string concatenation with ‘+’. This is the approach that I see many newcomers to JavaScript use in their code, as it is the simplest to implement.

Read More

Hover Effects in JavaScript?

One of the things that can be annoying when looking at someone else’s code is when a more complex technology is used to solve a problem that can be handled more simply with another method. An example of this is when you utilize JavaScript to implement a hover effect on some elements. I know of one scenario when you would need JavaScript to trigger a hover effect, and that would be when you want to trigger the hover effect with a touch event.

Read More

Avoid jQuery.bind()

When chasing down performance issues, you never know what kind of problems you will find. I was looking for something that would cause jitter when scrolling on the page. After looking at the custom code that runs on every scroll event, I still had not found a reason for the jitter. Looking at the JavaScript CPU profile when scrolling in Chrome showed that there was an overwhelming majority of the time spent in a function in the Prototype JS library.

Read More