[This is preliminary documentation and subject to change]
Creates a JScript package that enables the convenient packaging of named components.
package pname
{
[[vis] pmember]
}
pname
Required. The name of the package being created.
vis
Optional. Keyword (public or package) indicating the visibility of the member within the package. public members are visible by any code that imports the package. package members are not visible outside the package. If omitted, package members are public by default.
pmember
Optional. Zero or more classes
Only classes are allowed inside a package. However, functions are allowed within those classes.
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.
You cannot nest packages.
The following example defines two simple packages and imports them into the running script. Typically, each package would be in a separate file so as to allow maintenance and distribution of the package content. This example is notable because each package has a class containing a method whose name is the same as the corresponding method in the other package.
// Create a simple package containing a single method (sayHello).
package Deutschland
public class Greeting
{
public function sayHello() : String
{
return "Guten tag!";
}
}
};
// Create another simple package containing a single method (sayHello).
package France
{
public class Greeting
{
public function sayHello() : String
{
return "Bonjour!
}
};
// Import the Deutschland package.
import Deutschland;
// The following line works as expected and prints "Guten Tag".
print(new Greeting().sayHello());
// Import the France package.
import France;
// The following line of code causes an error because sayHello
// is ambiguous since a sayHello method exists in both packages.
print (new Greeting().sayHello());
// Declare a local class that shadows the imported classes.
class Greeting
{
function sayHello() : String
{
return "Greetings!";
}
}
// Prints "Greetings!" because local function takes precedence.
print(new Greeting().sayHello());
// Prints "Bonjour!" because of qualified reference to France package.
print(new France.Greeting().sayHello());
// Prints "Guten tag!" because of qualified reference to
// Deutschland package.
print (new Deutschland.Greeting().sayHello());