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.
Syntax
Type.registerNamespace( string );
Members
|
Name
|
Description
|
|
callBaseMethod Method
|
Invokes a base class method with specified arguments.
|
|
getBaseMethod Method
|
Returns the implementation of a method from the base class of the specified instance.
|
|
getBaseType Method
|
Returns the base type of an instance.
|
|
getInterfaces Method
|
Returns an Array object that contains the list of interfaces that the type implements.
|
|
getName Method
|
Returns the name of the instance type.
|
|
getRootNamespaces Method
|
Returns an Array object that contains references to all the root namespaces of the client application.
|
|
implementsInterface Method
|
Determines whether the type implements a specified interface.
|
|
inheritsFrom Method
|
Determines whether the type inherits from the specified parent 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.
|
|
isClass Method
|
Returns a value that indicates whether the specified type is a class.
|
|
isImplementedBy Method
|
Determines whether an instance implements the specified interface.
|
|
isInstanceOfType Method
|
Determines whether an object is an instance of either a specified type or of one of its derived types.
|
|
isInterface Method
|
Returns a value that indicates whether the specified type is an interface.
|
|
isNamespace Method
|
Returns a value that indicates whether the specified object is a namespace.
|
|
registerEnum Method
|
Registers a class as an enumeration.
|
|
parse Method
|
Returns an instance of the type that is specified by a type name.
|
|
registerClass Method
|
Registers a class that is defined by the constructor, with an optional base type and with interfaces.
|
|
registerInterface Method
|
Registers an interface that is specified by its constructor.
|
|
registerNamespace Method
|
Registers and creates a namespace.
|
|
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 , see Extending JavaScript with ASP.NET AJAX.
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.
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
The 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 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.
JavaScript
<!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>