Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
ASP.NET Reference
Client Reference
Global Namespace
Type Class
Type Methods
 registerClass Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Type.registerClass Method

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

classInstanceVar.registerClass(typeName, baseType, interfaceTypes)
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.

The registered type.

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.

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.

JavaScript
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.

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

<html  >
<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>


Reference

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
registerClass does not verify if Interfaces are implemented      Pascal_Dufresne   |   Edit   |   Show History
I have noticed that the registerClass function does not throw an error if the methods in the interfaces specified by the parameter interfaceTypes are not implemented in the class. Looking at the relevant section of the script code for the registerClass function we find:
if (interfaceTypes)
{
this.__interfaces = [];
this.resolveInheritance();
for (var i = 2, l = arguments.length; i < l; i++)
{
var interfaceType = arguments[i];
if (!interfaceType.__interface) throw Error.argument('interfaceTypes[' + (i - 2) + ']', Sys.Res.notAnInterface);
for (var methodName in interfaceType.prototype)
{
var method = interfaceType.prototype[methodName];
if (!this.prototype[methodName])
{
this.prototype[methodName] = method;
}
}
this.__interfaces.push(interfaceType);
}
}

Oddly, instead of throwing an error when an interface method is not implemented, it will copy the method in the prototype of the class ! Is there a reason for that ?

Pascal
http://csogateway.codeplex.com
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker