close
The Wayback Machine - https://web.archive.org/web/20160310125934/http://javascript.about.com/od/objectorientedjavascript/a/oop14.htm

Object Oriented JavaScript

14. Namespaces

JavaScript doesn't have namespaces as a part of the way the language is defined and so if we want to achieve the same result as could be achieved with namespaces then we need to find an alternative way to implement our code.

First off let's consider just what namespaces are and what they would give us. Basically namespaces are a way of grouping things together so that parts of one thing that just happen to have the same name as parts of some other thing don't get mixed up.

For example, both a book and a web page can have a title. Giving each their own namespace means that we can readily determine when referencing a title whether that title is a book title or a web page title. XML and XHTML do support namespaces and so we could readily use namespaces to determine which title is which in that instance by looking to see what namespace the title is in.

When using JavaScript with XML and XHTML we can specify which XML or XHTML namespace that we are trying to access. We just have no way of directly implementing equivalent namespaces within our JavaScript code itself.

By now you are probably wondering why this tutorial is a part of a series about object oriented JavaScript when namespaces don't really have anything to do with objects. Well the answer is that we can use objects to achieve an almost equivalent effect to what we would have if JavaScript supported namespaces.

What we can do is to createa JavaScript object to represent our namespace. All of the properties and methods that we want associated with our "namespace" are defined as properties and methods of that object.

In this particular instance the properties and methods that we are adding into our "namespace" object need not be related to one another in the way that they normally would be with a regular object. The important thing is to encapsulate all of the methods and properties that we would normally associate with a namespace within the one object.

Now instead of specifying a namespace for running our properties and methods we reference the object that they belong to instead.

This achieves almost the exact same result as we would get if JavaScript supported namespaces. The main difference is that the object itself is still in the global scope and the event handlers that trigger the various methods are also still in the global scope. There is still therefore an area for potential conflicts between scripts that we want to place in different namespaces since theose parts that cannot be removed from the global scope may be needed by more than one script leading to potential conflicts in that area.

Encapsulating as much as possible of our JavaScript into an object dramatically reduces the possibility of the script conflicting with other scripts in the same page. The only better way would be if JavaScript supported proper namespaces but at least by encapsulating everything possible we minimise the possibility of conflicts with other scripts.

All the Object Oriented JavaScript Tutorials

  1. What is Object Oriented JavaScript?
  2. The Benefits of OO JavaScript
  3. JavaScript's Built in Objects
  4. Extending Built In Objects
  5. Creating Objects from Existing Objects
  6. Creating New Objects Without Copying Existing Ones
  7. Dot Notation and "this"
  8. prototype
  9. Inheritance and "constructor"
  10. Associative Array Notation
  11. Create Method if Doesn't Exist
  12. "self" and Multiple Objects
  13. Defining Objects with JSON
  14. Namespaces
  15. Lazy Definition
  16. Extending Methods
  17. Copying Objects
  18. Private Properties and Privileged Methods
  19. Public Access to Private Methods
  20. Chaining Methods
  21. Singletons