Share via


Type.registerClass Method

Registers a class as defined by a constructor with an optional base type and interface type.

classInstanceVar.registerClass(typeName, baseType, interfaceTypes)

Parameters

  • typeName
    A string that represents the fully qualified name of the type.

  • baseType
    (Optional) The base type.

  • interfaceTypes
    (Optional) An unbounded array of interface type definitions that the type implements.

Return Value

The registered type.

Exceptions

Exception type

Condition

Error.argument Function

(Debug) typeName cannot be evaluated.

-or-

(Debug) baseType is not a class.

Error.invalidOperation Function

(Debug) The class specified by typeName is already registered.

Remarks

Use the registerClass method to register a class as defined by a constructor with an optional base class and interface type. The registerClass method is invoked after the class has been defined, but before it is instantiated. The registerClass method is invoked directly from the class.

Deriving from a Base Class

Methods of the Sys.Type class support single inheritance. You 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.

  • In the first line of the derived class definition, initialize the base class by calling the Type.initializeBase method.

The following example shows how to register a class that is derived from a base class. The code invokes the registerClass method with a base class specified in the baseType parameter. The derived class initializes its base class by calling the Type.initializeBase method in the first line of its definition.

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

Methods of the Type class support implementing an interface. You implement an interface by specifying a registered interface in the interfaceTypes parameter of the registerClass method when you register the class. For more information about how to register an interface, see Type.registerInterface Method.

The following example shows how to register a derived class. The registerClass method specifies a base class in the baseType parameter.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Sample</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:ScriptManager runat="server" ID="ScriptManager1">
       </asp:ScriptManager>

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

        Samples.A = function()
        {
            // Initialize as a base class.
            Samples.A.initializeBase(this);
        }

        Samples.B = function(){}
        Samples.C = function(){}

        Samples.A.registerClass('Samples.A');
        Samples.B.registerClass('Samples.B', Samples.A);

        Samples.C.registerClass('Samples.C');

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

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

        </script>
    </form>

</body>
</html>

See Also

Reference

Type Class