JScript .NET, Part III: Classes and Namespaces: Importing Namespaces - Doc JavaScript
JScript .NET, Part III: Classes and Namespaces
Importing Namespaces
The import
statement enables access to a namespace in an external library or within the current script. The syntax is very simple:
import namespaceName;
where namespaceName is the name of the required namespace. There is always the question as to where the computer should search for the given namespace. A namespace is a collection of classes that typically offer related features or functions. An assembly, on the other hand, is a physical file, deployed during installation of the .NET framework, or other applications. File extensions .dll
or .exe
are a clear indication of assemblies. Usually, your namespaces will be implemented in .dll
files. What's the relationship between the .dll
file name and the namespace name? Generally speaking, none. A single namespace can be distributed among several .dll
files. Also, a single .dll
file can include multiple namespaces. In practice, though, the name of the .dll
is usually the name of the namespace, and the jsc.exe
compiler does support this practice with the /autoref
compilation switch.
There are clear rules as to where to search for imported namespaces when they are not defined in the current script. For example, if you import the namespace System.Environment.SpecialFolder
and it is not defined within the current script, the jsc.exe
compiler will look for the required namespace in the following assemblies (.dll
files), in this order:
System.Environment.SpecialFolder.dll System.Environment.dll System.dll
You can avoid any linkages between namespaces and assemblies in two ways. First, you can specify /autoref-
at the jsc.exe
invocation. Another way is to import a namespace that does not have a corresponding assembly. But now you need to specify the assembly explicitly. You do this with the /reference
switch of the jsc.exe
compiler. Here is an example:
jsc /reference:privatedll.dll myscript.js
When you import several namespaces in your code, and you reference a class member (a property or method), there is always the question from which namespace this class is referenced. The compiler first searches the local scope. If the class is not found, the compiler searches the classes in each of the imported namespaces, in the order they were imported, and stops when it finds a match.
You can always control the namespace you want to reference by using a fully qualified name. Here is an example:
import System.Collections; var my_queue : System.Collections.Queue;
Assuming several namespaces include different types of the Queue
class, you can use the fully-qualified name to force usage of an explicit namespace, the System.Collections
namespace. But if you are sure the class Queue
is defined only once in all imported namespaces, you can use a shorter non-qualified name:
import System.Collections; var my_queue : Queue;
You may mix fully-qualifed and non-qualified class names. The advantage of fully-qualified names is that the code is much easier to debug because you know exactly where the classes reside.
Next: How to package a namespace
Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: May 6, 2002
Revised: May 6, 2002
URL: https://www.webreference.com/js/column109/3.html