May 26, 2002 - Handling Non-Qualified Name Collisions | WebReference

May 26, 2002 - Handling Non-Qualified Name Collisions

Yehuda Shiran May 26, 2002
Handling Non-Qualified Name Collisions
Tips: May 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

When you import classes from outside your code, there is always a possibility of class and variable collisions, if you use non-qualified names. Non-qualified names sometimes do not uniquely identify a class or a class member, because these classes are defined in multiple namespaces. In such cases of ambiguity, the local class takes over the imported class. The following USA.Florida namespace defines the class Head, which includes the Governor function:

  package USA.Florida {
    public class Head {
      static var Governor : String = "Bush";
    }
  };
The following code defines a local class and a local function. They are named by the same names as above:

   class Head {
      static var Governor : String = "Davis";
   }
Now, we import USA.Florida, conflicting the definition of Head and Governor:

  import USA.Florida;
The class Head.Governor is non-qualified, because there is such a class in the USA.Florida namespace, and there is also a class by the same name in the local namespace. When you print the value of the non-qualified function Head.Governor, the local value will override the imported name. The code:

  print(Head.Governor);
will yield the following output:

  Davis
To learn more about JScript .NET, go to Column 109, JScript .NET, Part III: Classes and Namespaces.