Freigeben über


Type Class

Provides a typing and type-reflection system for ECMAScript (JavaScript) object-oriented programming functionality.

Namespace: None. The Type methods are global and not part of a namespace.

Inherits: window.

Type.registerNamespace( string );

Members

Name

Description

Type.callBaseMethod Method

Invokes a base class method with specified arguments.

Type.createCallback Method

Creates a callback method, given the function to callback and the parameter to pass to it.

Type.createDelegate Method

Creates a delegate function that keeps the context from its creation. The context defines the object instance to which the this keyword points.

Type.getBaseMethod Method

Returns the implementation of a method from the base class of the specified instance.

Type.getBaseType Method

Returns the base type of an instance.

Type.getInterfaces Method

Returns an Array object that contains the list of interfaces that the type implements.

Type.getName Method

Returns the name of the instance type.

Type.getRootNamespaces Method

Returns an Array object that contains references to all the root namespaces of the client application.

Type.implementsInterface Method

Determines whether the type implements a specified interface.

Type.inheritsFrom Method

Determines whether the type inherits from the specified parent type.

Type.initializeBase Method

Initializes the base class and its members in the context of a given instance, providing the model for inheritance and for initializing base members.

Type.isClass Method

Returns a value that indicates whether the specified type is a class.

Type.isEnum Method

Indicates whether the specified type is an enumeration.

Type.isFlags Method

Get a value that indicates whether the specified type is an integer of flags.

Type.isImplementedBy Method

Determines whether an instance implements the specified interface.

Type.isInstanceOfType Method

Determines whether an object is an instance of either a specified type or of one of its derived types.

Type.isInterface Method

Returns a value that indicates whether the specified type is an interface.

Type.isNamespace Method

Returns a value that indicates whether the specified object is a namespace.

Type.parse Method

Returns an instance of the type that is specified by a type name.

Type.registerClass Method

Registers a class that is defined by the constructor, with an optional base type and with interfaces.

Type.registerEnum Method (Microsoft Ajax)

Registers an enumeration.

Type.registerInterface Method

Registers an interface that is specified by its constructor.

Type.registerNamespace Method

Registers and creates a namespace.

Type.resolveInheritance Method

Copies members from the base class to the prototype that is associated with the derived class, and continues this process up the inheritance chain. This enables you to reflect on the inherited members of a derived type.

Remarks

The methods of the Type class add object-oriented programming functionality to ECMAScript (JavaScript) code by creating a typing system and by enabling type reflection. This class enables you to register code relationships and structure, which includes classes, interfaces, base classes, and enumerations. You can use the Type reflection methods such as isInstanceOfType and isImplementedBy for inspecting classes. The built-in JavaScript Function object is aliased to Type. Therefore, many of the Type methods are available for your own custom type objects created through the Type APIs.

For an overview of the object-oriented programming model in Microsoft Ajax Library, see Creating Custom Client Script by Using the Microsoft Ajax Library.

Deriving From a Base Class

The Type methods support only single inheritance but allow multiple interfaces. Derive one class from another by following these steps:

  • When you register the derived class, specify a base class in the baseType parameter of the registerClass method.

  • Initialize the derived class's base by invoking the initializeBase method from the constructor of the derived class.

Calling a Base Class Method From a Derived Class Member

You call base class methods from derived class members by invoking the Type.callBaseMethod method. You do this when you want to override methods. For example, you call the base class if you override the Sys.Component.dispose method in a derived class.

The following code shows how to register a class that is derived from a base class. You call the registerClass method of the type that is being registered. The base class is specified in the baseType parameter. The derived class initializes its base class by calling the initializeBase method from its constructor.

Type.registerNamespace('Samples');

Samples.A = function(){}
// Register Samples.A Class
Samples.A.registerClass('Samples.A');

Samples.B = function(){}
// Register Samples.B Class
Samples.B.registerClass('Samples.B');

Samples.C = function(){
    // Initialize the base.
    Samples.C.initializeBase(this);
}
// Register Samples.C Class as derviving from Samples A and implementing Samples.B
Samples.C.registerClass('Samples.C', Samples.A, Samples.B);

Implementing an Interface

The Microsoft Ajax Library Type methods support interfaces. You implement an interface when you call the registerClass method by specifying a registered interface in the interfaceTypes parameter. For more information about registering an interface, see Type.registerInterface Method.

Example

The following example shows how to create a derived class that implements an interface. The code invokes the registerClass method with a base class specified in the baseType parameter and an interface specified in the interfaceTypes parameter. The base class is initialized in the context of a given instance by invoking initializeBase from the constructor of the derived class. The implementsInterface method is then called to determine the interface of a class instance.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Samples</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" ID="ScriptManager01">
        </asp:ScriptManager>

         <script type="text/javascript">
            // Register classes to test.
            Type.registerNamespace('Samples');

            Samples.A = function(){}
            // Register Samples.A Class
            Samples.A.registerClass('Samples.A');

            Samples.B = function(){}
            // Register Samples.B Class
            Samples.B.registerClass('Samples.B');

            Samples.C = function(){
                // Initialize the base.
                Samples.C.initializeBase(this);
            }
            // Register Samples.C Class as derviving from Samples A and implementing Samples.B
            Samples.C.registerClass('Samples.C', Samples.A, Samples.B);

            var isDerived;
            isDerived = Samples.B.inheritsFrom(Samples.A);
            // Output: "false".
            alert(isDerived);

            isDerived = Samples.C.inheritsFrom(Samples.A);
            // Output: "true".
            alert(isDerived);

            var implementsInterface
            implementsInterface = Samples.C.implementsInterface(Samples.B);
            // Output: "true".
            alert(implementsInterface);

         </script>
    </form>
</body>
</html>

See Also

Reference

new Operator

Other Resources

Language Reference