Namespace (Definition)

A table where identifiers are stored.

A namespace behaves like a dictionary table where lexically sorted items can be stored. There may be several namespaces. Each namespace is distinct from any other and so a particular value may be present in more than one namespace and may mean a different thing in each.

For example, the collection of properties belonging to an object class is referenced via a namespace. Each class maintains a separate namespace and so a property with a particular name can be added to several objects without any collisions.

The namespace where variable names and functions belong is also the same namespace where reserved words and keywords are accessed. Or at least this is the conceptual idea, when the standard recommends that you should not name your identifiers with the same value as a reserved word.

A namespace could be conceived as a particular directory within a file system or a set of object ID names within a document model. All of these operate with typical namespace behaviors regarding collisions between dissimilar objects having the same name.

Namespace pollution can be alleviated by prefixing the names of certain kinds of objects. You might have two functions that create named items in a namespace and to ensure they do not operate on each other's entities, they could each add a different prefixing letter to the entity names.

Style sheets use namespaces as well to indicate different functionality during the cascading process.

The scope-chain rules of execution contexts within a JavaScript allow for namespaces to be expanded and collapsed as functions are called and executed. Variables can be created with a scope that is local to within a specific function. However the scope-chain mechanism allows the namespace to be layered and cascaded so that a reference to an identifier that is not local to the function but exists higher up the scope chain is still reachable. This means that the namespace for variables in JavaScript operates in a tree-like manner.

You may find in some implementations that there are small variations in the namespace behavior. Historically, language developers have found this to be a contentious area. In particular, there was a stage during the development of the C language standard where members of all structures were considered to live within the same namespace. Fortunately, the standard rejects this model and members of structures live in separate namespaces, one per structure. So it should be for objects in JavaScript, given the caveat that prototype inheritance can cloud the issue and a prototype tree for objects operates essentially in the same way as the scope chain does for functions.

Typically the object, method and property names are spelled inconsistently for some items. There are wide spread inconsistencies in the JavaScript language implementations. Although the core language is specified reasonably consistently, the browser manufacturers have added a large number of extensions that do not follow consistent naming conventions.

See also:Identifier, Member,, Prototype Based Inheritance, Scope chain

insert figure 0039