In this article we will look some very nice features provided by Asp.net ajax library.
Introduction
In this article I will discuss a JavaScript object model
provided by asp.net ajax framework. First let’s have look on basic JavaScript techniques to
create closures and prototype.
Closure
A closure is a function that encapsulates a
class.e.g.
To access class members we can create object of the class
just like we do for any other oop language.
e.g.
Prototype
Prototype is another approach for designing
classes in JavaScript. This is most preferred approach as it enhances performance.
Using prototype method we can design classes as belowe.g.
Now we have seen the prototype method of declaring a
class. Object creation, accessing members is same as that of seen for closure.
one might ask what is the difference between
closure method and prototype method? Answer is, closure creates members for
your object every time it is created but for prototype members are created only
once and then simply copied in each n new object. Hence performance wise prototype
method is more suitable.
Working With Asp.Net Ajax Library
ASP.Net ajax provide some very useful
features for supporting object oriented development.Using these features we can
-
Create namespaces
- Create classes
- Create interfaces
- Create
enumarations
- Inherit classes and interfaces
- Use reflections
And much more. Here I will write few basic
things which I have studied and which you find as a basic for learning Ajax
library
Registering Class with Asp.Net Ajax library
We saw how to create classes using JavaScript
prototype approach. Now we have to register this class with Ajax library. How
to do that?For doing this first we need to include
ASP.Net Ajax control named
ScriptManager to
our page.(Its just drag and drop from control
toolbox). Script manager will automatically include necessary JavaScript
libraries for you. Now care must be taken for whatever code we will write it
should be written below the script Manager.
For registering class we use
registerClass() function.
e.g.
After this step your class is registered with Asp.net
Ajax library and can now use inbuilt features provided by Ajax framework like
reflection
This will return you a fully qualified name of a
class the object belong to. Additional methods are listed below
Creating And Registering Namespace
To register a namespace we use
Type.registerNamespace() method.
e.g.
Here if you apply
Object.getTypeName() function on object obj then it will return
fully qualified name i.e.
Animals.Mammals
Inheritance
Inheritance can be achieved using
registerClass() method.
Syntax :
classInstanceVar.registerClass(typeName,
baseType, interfaceTypes)
Where,
typeName = fully qualified name of class
baseType= this is optional parameter. Base class
name
interfaceTypes= this is optional parameter.(see
it is types not type). Interface name.
if more than one then separate names
using comma.
This can be best understood using an example.
e.g.
Implementing Interfaces
Interfaces
allow us to implement a common set of methods across multiple classes with less
room for error.
Below example shows an interface "IShape" which contain an abstract method
named drawShape().I have created IShape interface and a class Circle which
implement the interface.
After creating an interface we need to
register it with clientside ajax library using registerInterface()
method.(last code line)
Now
to implement an interface first give implementation for its method(s) and then
inherit the class from interface in registerClass() method.
e.g.
null parameter in registerClass() in above code indicate
that there is no base class for this class.
if you run the code you will get the following output
Creating Enumerations
Creating enum types with ajax library is very simple.
first create enum and the register it with ajax library using Type.registerEnum()
method. Look at below example where Enum named color is created and registered
with Ajax library
References
http://www.asp.net/AJAX/Documentation/Live/ClientReference/Global/TypeClass/TypeRegisterClassMethod.aspx.