July 2000 Draft
JavaScript 2.0
Core Language
Namespaces
previousupnext

Tuesday, June 6, 2000

Namespace Definition

NamespaceDefinition  namespace [no line break] Identifier ExtendsList

This defines a new namespace named Identifier.

Use Statement

UseStatement  use [no line break] namespace NonassignmentExpressionList
NonassignmentExpressionList 
   NonAssignmentExpressionallowIn
|  NonassignmentExpressionList , NonAssignmentExpressionallowIn

This makes the contents of each listed namespace accessible without a qualifier.

General Name Lookup

When performing the member lookup x.q1::q2::q3::n, all properties of x named n are gathered into a set S. All properties except those that belong to each of the namespaces, classes, or interfaces q1, q2, q3 are dropped. Then, all properties defined in a namespace N that neither is used by a lexically enclosing scope nor is one of the namespaces q1, q2, q3 (or their supernamespaces) are dropped. If only one property remains, then that's the one that is chosen. If multiple properties remain, then ones defined in the a namespace that is used in the innermost enclosing scope are chosen. If multiple properties still remain, then the ones defined in the most specific class are chosen (this gives object-oriented semantics). If multiple properties still remain, an error occurs.

If the above member lookup was done on behalf of the [] operator, all nonindexable properties of x would also have been dropped from the set S.

When performing the variable lookup q1::q2::q3::n, all entities named n defined in lexically enclosing scopes (such that the variable lookup is in the definitions' static extents) are gathered into a set S. All entities except those that belong to each of the namespaces, classes, or interfaces q1, q2, q3 are dropped. Then, all implicit entities defined in a namespace N that neither is used by a lexically enclosing scope nor is one of the namespaces q1, q2, q3 (or their supernamespaces) are dropped. All explicit entities defined in a namespace N that is not one of the namespaces q1, q2, q3 (or their supernamespaces) are dropped. If only one entity remains, then that's the one that is chosen. If multiple entities still remain, then the ones defined in the most specific scope are chosen (inner definitions hide outer ones). If multiple entities still remain, an error occurs.

Class Extensions

Under limited circumstances it is possible to add methods to an existing class C even if C is in an already defined package. To do this one creates a new namespace N and places the methods qualified with C into N. Such methods have no special access privileges to view C's internals (for example, they can't see C's original private or package members), and they can only be used by code that uses N. Because the slots of C's instances are already fixed, it is not possible to add instance variables this way, but it is possible to add getters and setters when extending C.

An extension is useful to add methods to system classes, as in the following code in some user package P:

namespace StringExtension;
attribute SE = namespace(StringExtension);

SE function String::scramble():String {...}
SE function String::unscramble():String {...}

use namespace StringExtension;

var x:String = "abc".scramble();

Once the class extension is evaluated, methods scramble and unscramble become available on all strings in code within the scope of a use namespace  StringExtension. There is no possibility of name clashes with extensions of class String in other, unrelated packages because the names scramble and unscramble only acquire their special meanings when qualified by the namespace StringExtension.


Waldemar Horwat
Last modified Tuesday, June 6, 2000
previousupnext