Analysis of the object-oriented idea of ASP. NET Ajax

Source: Internet
Author: User
The long-awaited official ASP. NET Ajax V1.0 version has finally been released. Now, you can easily write rich and interactive web applications using Microsoft ASP. NET Ajax JavaScript. In particular, it is worth noting that the Microsoft Ajax library has added object-oriented support, whereas JavaScript previously did not support object-oriented development. Currently, icrosoft Ajax Library supports features such as class, namespace, inheritance, interfaces, enumeration, and reflection. These new functions are similar to. NET Framework, which makes it easy to maintain and expand ASP. NET Ajax applications. Now let's take a look at how Microsoft Ajax Library supports the above features.
1. Class, member, and namespace
In Microsoft Ajax library, all JavaScript classes are inherited from objects (similar. net Framework library, all inherited from the object), in ASP. net Ajax applications you can use the object-oriented programming mode to create objects and components inherited from the Microsoft Ajax base class. The class has four types of members: fields, attributes, methods, and events. Fields and attributes are name/value pairs used to describe the characteristics of an instance of a class. Fields are composed of simple types and can be accessed directly. For example:
Myclassinstance. Name = "Fred ".
The property can be of any simple type or reference type and accessed through the get and set methods. In ASP. in net Ajax, get and set are independent functions, and the prefix "GET _" or "SET _" must be used in the function name _", for example, to obtain or set the value of the cancel attribute, you can call the get_cancel or set_cancel method.
One method is to complete an activity function instead of returning the value of an attribute. The attributes and methods are demonstrated in the following example.
Event indicates the occurrence of a specific action. When an event occurs, it can call one or more functions. The event owner can complete any task waiting for the event to occur.
A namespace is a logical group of associated classes. Namespace allows you to group public functions.
To enable ASP. net web page has ASP. net Ajax function, you must add the <asp: scriptmanager> control to the page. When the page is started, see ASP.. Net Ajax library scripts are automatically generated.
The following example shows that the <asp: scriptmanager> control is used on the page.
<Asp: scriptmanager runat = "server" id = "scriptmanager"/>
The following example demonstrates how to use the type. registernamespace and. registerclass methods to add the person class to the demo namespace, create a class, and then register the class.
Type. registernamespace ("Demo ");

Demo. Person = function (firstname, lastname, emailaddress ){
This. _ firstname = firstname;
This. _ lastname = lastname;
This. _ emailaddress = emailaddress;
}

Demo. Person. Prototype = {

Getfirstname: function (){
Return this. _ firstname;
},

Getlastname: function (){
Return this. _ lastname;
},

Getname: function (){
Return this. _ firstname + ''+ this. _ lastname;
},

Dispose: function (){
Alert ('bye' + this. getname ());
}
}
Demo. Person. registerclass ('demo. person', null, SYS. idisposable );
In the script file namespace. JS, the class person is defined and the class namespace is set to "Demo ". Run namespace. aspx on the page. click the button to create an instance of the demo. Person class.

2. Access Modification
Many object-orientedProgramming LanguageAll have access modification concepts. Allows you to specify a class or member to be valid within a certain range. For exampleProgram, Internal class with the same namespace or specifiedCodeFast class. No access modifier is provided in Javascript, but in ASP. NET Ajax, "_" starting with the following underlined character is considered private, and the class cannot be accessed externally.
3. Inheritance
Inheritance is the ability of a class derived from another class. The derived class automatically inherits all fields, attributes, methods, and events of the base class. A derived class can add new members or override existing Members of the base class to change the behavior of members.
The following script instances have two classes: person and employee, which are inherited from person. The two classes demonstrate the use of private fields, both of which have common attributes and methods. In addition, the employee class overrides the tostring Implementation of the person class and calls the function of the base class.
Type. registernamespace ("Demo ");

Demo. Person = function (firstname, lastname, emailaddress ){
This. _ firstname = firstname;
This. _ lastname = lastname;
This. _ emailaddress = emailaddress;
}

Demo. Person. Prototype = {
Getfirstname: function (){
Return this. _ firstname;
},

Getlastname: function (){
Return this. _ lastname;
},

Getemailaddress: function (){
Return this. _ emailaddress;
},
Setemailaddress: function (emailaddress ){
This. _ emailaddress = emailaddress;
},

Getname: function (){
Return this. _ firstname + ''+ this. _ lastname;
},

Dispose: function (){
Alert ('bye' + this. getname ());
},

Sendmail: function (){
VaR emailaddress = This. getemailaddress ();

If (emailaddress. indexof ('@') <0 ){
Emailaddress = emailaddress + '@ example.com ';
}
Alert ('sending mail to '+ emailaddress + '...');
},

Tostring: function (){
Return this. getname () + '(' + this. getemailaddress () + ')';
}
}
Demo. Person. registerclass ('demo. person', null, SYS. idisposable );
Demo. Employee = function (firstname, lastname, emailaddress, team, title ){
Demo. employee. initializebase (this, [firstname, lastname, emailaddress]);

This. _ Team = team;
This. _ Title = title;
}

Demo. employee. Prototype = {

Getteam: function (){
Return this. _ Team;
},
Setteam: function (Team ){
This. _ Team = team;
},

Gettitle: function (){
Return this. _ title;
},
Settitle: function (title ){
This. _ Title = title;
},
Tostring: function (){
Return demo. employee. callbasemethod (this, 'string') + '\ r \ n' + this. gettitle () +' \ r \ n' + this. getteam ();
}
}
Demo. employee. registerclass ('demo. Employee ', demo. person );

The inheritance. js script file defines two classes: person and employee, which are inherited from person. Each class has fields, public attributes, and methods. In addition, the employee class overrides the tostring implementation and calls the function of the base class in the rewritten code. In this example, set the namespace of the class person to "Demo ". Running page inheritance. aspx: click "create object", "Object release", "Public and Private attributes", "Object method", and "override method" to check the object type.
4. Interface
An interface is the logical protocol to be implemented by a class and a public compliance for class integration. It allows multiple classes and the same interface to combine implementation definitions with specific implementation of classes. The following example defines a base class tree and an interface ifruittree. Apple and banana implement the interface ifruittree, but the pine class does not implement the interface ifruittree.
Type. registernamespace ("demo. Trees ");

Demo. Trees. ifruittree = function (){}
Demo. Trees. ifruittree. Prototype = {
Bearfruit: function (){}
}
Demo. Trees. ifruittree. registerinterface ('demo. Trees. ifruittree ');

Demo. Trees. Tree = function (name ){
This. _ name = Name;
}
Demo. Trees. Tree. Prototype = {
Returnname: function (){
Return this. _ name;
},

Tostringcustom: function (){
Return this. returnname ();
},

Makeleaves: function (){}
}
Demo. Trees. Tree. registerclass ('demo. Trees. trees ');

Demo. Trees. fruittree = function (name, description ){
Demo. Trees. fruittree. initializebase (this, [name]);
This. _ description = description;
}
Demo. Trees. fruittree. Prototype. bearfruit = function (){
Return this. _ description;
}
Demo. Trees. fruittree. registerclass ('demo. Trees. fruittree ', demo. Trees. Tree, demo. Trees. ifruittree );

Demo. Trees. Apple = function (){
Demo. Trees. Apple. initializebase (this, ['apple', 'Red and crunchy']);
}
Demo. Trees. Apple. Prototype = {
Makeleaves: function (){
Alert ('medium-sized and desiduous ');
},
Tostringcustom: function (){
Return 'fruittree '+ demo. Trees. Apple. callbasemethod (this, 'tostringcustom ');
}
}
Demo. Trees. Apple. registerclass ('demo. Trees. apple', demo. Trees. fruittree );

Demo. Trees. grannysmith = function (){
Demo. Trees. grannysmith. initializebase (this );
// You must set the _ description feild after initializebase
// Or you will get the base value.
This. _ description = 'green and sour ';
}
Demo. Trees. grannysmith. Prototype. tostringcustom = function (){
Return demo. Trees. grannysmith. callbasemethod (this, 'tostringcustom') + '... its grannysmith! ';
}
Demo. Trees. grannysmith. registerclass ('demo. Trees. grannysmith ', demo. Trees. Apple );

Demo. Trees. Banana = function (description ){
Demo. Trees. Banana. initializebase (this, ['bana', 'Yellow and squishy ']);
}
Demo. Trees. Banana. Prototype. makeleaves = function (){
Alert ('Big and green ');
}
Demo. Trees. Banana. registerclass ('demo. Trees. Banana ', demo. Trees. fruittree );

demo. trees. pine = function () {
demo. trees. pine. initializebase (this, ['pine']);
}< br> demo. trees. pine. prototype. makeleaves = function () {
alert ('needles in clusters');
}< br> demo. trees. pine. registerclass ('demo. trees. pine ', demo. trees. tree);
interface. the JS script file defines a tree base class and an ifruittree interface. The inheritance classes apple and banana implement the ifruittree interface, while the pine class does not implement the ifruittree interface. Run interface. aspx and click "Object creation", "Interface Check", and "call interface method" to experience the operation.
5. Enumeration
enumeration contains a group of classes named Positive Integer constants. You can access its value just like an access attribute. For example,
myobject. Color = mycolorenum. Red, enumeration provides an integer representation that is easy to understand. The following example defines an enumeration type in which a color in hexadecimal notation is named as a meaningful name.
type. registernamespace ("Demo");

// define an enumeration type and register it.
demo. color = function () {};
demo. color. prototype =
{< br> Red: 0xff0000,
Blue: 0x0000ff,
Green: 0x00ff00,
White: 0 xffffff
}< br> demo. color. registerenum ("demo. color ");
RUN enumeration. aspx: select the color in the drop-down box. The script sets the background color to the demo of the selected enumeration type. color.
6. reflection
reflection is used to check the structure and composition of a runtime program. Reflection is implemented through the type API, these methods enable you to collect information about an object, such as: Which class it inherits, whether it implements a specific interface of the class, and whether it is an instance of a class.
the following example uses the reflected API to test whether the grannysmith class has implemented the previous interface. Run reflection. aspx and click "Check type", "Check inheritance", and "Check interface.
In addition, Microsoft Ajax library is developed based on the open architecture, not only for Asp.net, but also for other architecture, such as Java.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.