Export (0) Print
Expand All

package Statement

Creates a JScript package that enables the convenient packaging of named components.

package pname {
   [[modifiers1] pmember1]
   ...
   [[modifiersN] pmemberN]
}

pname

Required. The name of the package being created.

modifiers1, ..., modifiersN

Optional. Modifiers that control the visibility and behavior of the pmember.

pmember1, ..., pmemberN

Optional. Class, interface, or enumeration definition.

Only classes, interfaces, and enumerations are allowed inside a package. Package members may be marked with visibility modifiers to help control access to the member. In particular, the internal modifier marks a member as being visible only within the current package.

Once a package has been imported, package members can be accessed directly by name, except when a member has the same name as another declaration visible to the importing scope. When that happens, the member must be qualified using its package name.

JScript does not support declaring nested packages; only class, interface, and enumeration declarations may appear inside a package. A package name can include a '.' character to indicate that it should be considered as nested in another package. For example, a package named Outer and a package named Outer.Inner do not need to have a special relationship to each other; they are both packages at the global scope. However, the names imply that Outer.Inner should be considered as nested within Outer.

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

Community Additions

ADD
Show:
© 2014 Microsoft