Share via


import Statement

Enables access to a namespace contained either within the current script or in an external library.

import namespace

Arguments

  • namespace
    Required. Name of the namespace to import.

Remarks

The import statement creates a property on the global object with the name supplied as namespace and initializes it to contain the object that corresponds to the namespace being imported. Any properties created using the import statement cannot be assigned to, deleted, or enumerated. All import statements are executed when a script starts.

The import statement makes a namespace available to your script. The namespace may be defined in the script by using the package statement, or an external assembly may provide it. If the namespace is not found within the script, JScript searches for an assembly that matches the name of the namespace in the specified assembly directories, unless the program is being compiled and the /autoref option is turned off. For example, if you import the namespace Acme.Widget.Sprocket and the namespace is not defined within the current script, JScript will search for the namespace in the following assemblies:

  • Acme.Widget.Sprocket.dll

  • Acme.Widget.dll

  • Acme.dll

You can explicitly specify the name of the assembly to include. This must be done if the /autoref option is turned off or if the name of the namespace does not match the assembly name. The command line compiler uses the /reference option to specify the assembly name, while ASP.NET uses the @ Import and @ Assembly directives to accomplish this. For example, to explicitly include the assembly mydll.dll, from the command line you would type

jsc /reference:mydll.dll myprogram.js

To include the assembly from an ASP.NET page, you would use

<%@ Import namespace = "mydll" %>
<%@ Assembly name = "mydll" %>

When a class is referenced in code, the compiler first searches for the class in the local scope. If the compiler finds no matching class, the compiler searches for the class in each namespace, in the order in which they were imported, and stops when it finds a match. You can use the fully qualified name of the class to be certain from which namespace the class derives.

JScript does not automatically import nested namespaces; each namespace must be imported using the fully qualified namespace. For example, to access classes from a namespace named Outer and a nested namespace named Outer.Inner, both namespaces must be imported.

Example

The following example defines three simple packages and imports the namespaces into the script. Typically, each package would be in a separate assembly to allow maintenance and distribution of the package content.

// Create a simple package containing a class with a single field (Hello).
package Deutschland {
   class Greeting {
      static var Hello : String = "Guten tag!";
   }
};
// Create another simple package containing two classes.
// The class Greeting has the field Hello.
// The class Units has the field distance.
package France {
   public class Greeting {
      static var Hello : String = "Bonjour!";
   }
   public class Units {
      static var distance : String = "meter";
   }
};
// Use another package for more specific information.
package France.Paris {
   public class Landmark {
      static var Tower : String = "Eiffel Tower";
   }
};

// Declare a local class that shadows the imported classes.
class Greeting {
   static var Hello : String = "Greetings!";
}

// Import the Deutschland, France, and France.Paris packages.
import Deutschland;
import France;
import France.Paris;

// Access the package members with fully qualified names.
print(Greeting.Hello);
print(France.Greeting.Hello);
print(Deutschland.Greeting.Hello);
print(France.Paris.Landmark.Tower);
// The Units class is not shadowed, so it can be accessed with or without a fully qualified name.
print(Units.distance);
print(France.Units.distance);

The output of this script is:

Greetings!
Bonjour!
Guten tag!
Eiffel Tower
meter
meter

Requirements

Version .NET

See Also

Reference

package Statement

/autoref

/lib

@ Assembly

@ Import