Skip to content Skip to navigation

OpenStax-CNX

You are here: Home » Content » Namespace pattern

Navigation

Recently Viewed

This feature requires Javascript to be enabled.
 

Namespace pattern

Module by: Hannes Hirzel. E-mail the author

Summary: This modules explains how to avoid having too many global variables in JavaScript programs. This is accomplished by using the 'namespace pattern'. This pattern is used in many libraries.

Introduction

In this module a "pattern" is described which explains how to reduce the number of identifiers in JavaScript on the global scope level. This is useful if you want to write a library and avoid a clash of names with the user of your library.

The problem

Let us consider the following code in JavaScript.

var n=3;
var x=0;
var text="abc";

var open = function(anArg) {
    console.log('within the "open" function');
    console.log("text=" + text);
    console.log("n=" + n);
    }

var doCalculation = function(i,j) {
    console.log('within doCalculation function');
};

open(text);

When we run the code in a web console or better in a scratch pad we get the following result


[07:39:08.141] within open function 
[07:39:08.145] text=abc 
[07:39:08.148] n=3 

The problem is that we now have five identifier on the global level.

Identifier

  • n
  • x
  • text
  • open
  • doCalculation
If we add another JavaScript library it is likely that it might contain identifiers with the same name. This leads to a clash of names.

We want to keep the number of identifiers declared at a global level to a minimum.

Solution

We can reduce the number of global identifiers to one. This will be the namespace identifier. We declare an object NSP1 to be an empty JavaScript object.


var NSP1 = {};

Then we add our variable and function declarations as properties and methods of NSP1.

NSP1.n=3;
NSP1.x=0;
NSP1.text="abc";

NSP1.open = function(anArg) {
    console.log('within open function');
    console.log("text=" + this.text);
    console.log("n=" + this.n);
    }

NSP1.doCalculation = function(i,j) {
    console.log('within doCalculation function');
};

NSP1.open(text);

Result

We have successfully reduced the number of global variables from 5 to 1. The only downside is that we have to prefix the reference to the variables with NSP1.

References

Axel Rauschmayer, Patterns for modules and namespaces in JavaScript

Stoyan Stefanov, JavaScript Patterns, O'Reilly 2010, 'Namespace pattern', p. 87

Avoid globals (Opera Developer Article)

Addy Osmani, JavaScript patterns; Singleton pattern

Content actions

Download module as:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

Definition of a lens

Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks