Use Microsoft Ajax Library to create a custom client script

Source: Internet
Author: User
Document directory
  • Interface
  • Use Enumeration
  • Reflection usage

MSDN address: http://msdn.microsoft.com/zh-CN/library/bb386453.aspx

Microsoft Ajax Library is a client-based Ajax js Library provided by Microsoft. By adding the ScriptManager control to the page, you can manage these scripts and any custom scripts.

All content introduced in this section depends on this JS library and has nothing to do with server development!

MS Ajax Library has some functions:

  • The object-oriented function is added to js. js code can be organized using classes, namespaces, and inheritance.
  • The reflection function checks the structure and components of the client script at runtime.
  • Enumeration
  • Extends the base type of JS to shorten development time.
  • Better debugging and tracking functions.
JS object-oriented usage

The Type class adds object-oriented functions such as namespaces, classes, and inheritance for JavaScript programming. Any JavaScript Object registered using the Type class will automatically obtain the permission to access this function.

// Register the namespace Type. registerNamespace ("Demo"); // Add the Person class to the namespace, followed by the constructor Demo of Person. person = function (firstName, lastName, emailAddress) {this. _ firstName = firstName; this. _ lastName = lastName; this. _ emailAddress = emailAddress;} // Demo of the Person class method. 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 () + ')' ;}// registration Demo. personDemo. person. registerClass ('demo. person ', null, Sys. IDisposable); // defines the Demo of the Employee class. employee = function (firstName, lastName, emailAddress, team, title) {Demo. employee. initializeBase (this, [firstName, lastName, emailAddress]); this. _ team = team; this. _ title = title;} // Demo of the Employee class method. 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, 'tostring') + '\ r \ n' + this. getTitle () + '\ r \ n' + this. getTeam () ;}// register Demo. and specify that the class inherits from the Demo. person Demo. employee. registerClass ('demo. employee ', Demo. person );

 

Call code:

var person = new Demo.Person('Jack', 'Smith', 'jack@hotmail.com');alert(person.getEmailAddress());var employee = new Demo.Employee('Jack', 'Smith', 'jack@hotmail.com', 'Web Dev', '');alert(employee.getTeam());
Interface
// Register the Demo interface. Trees. IFruitTree. registerInterface ('demo. Trees. IFruitTree ');
// Use the interface // class Demo. trees. fruitTree inherits the Demo. trees. tree, and implements the interface Demo. trees. IFruitTreeDemo. trees. fruitTree. registerClass ('demo. trees. fruitTree ', Demo. trees. tree, Demo. trees. IFruitTree );
Use Enumeration
// Register the namespace Type. registerNamespace ("Demo"); // defines the Demo of the enumeration type. color = function () {}; Demo. color. prototype = {Red: 0xFF0000, Blue: 0x0000FF, Green: 0x00FF00, White: 0 xFFFFFF} // register an enumeration Demo. color. registerEnum ("Demo. color "); // usage var color = Demo. color. red;

 

Reflection usage

Reflection refers to the ability to check program structures and components at runtime. The API that implements reflection is an extension of the Type class. Using these methods, you can collect information about an object, such as who the object inherits, whether it implements a specific interface, and whether it is an instance of a specific class.

Var g = new Demo. trees. greenApple (); var gt = Demo. trees. greenApple; var a = new Array (Demo. trees. apple, Demo. trees. tree, Demo. trees. pine, Demo. trees. IFruitTree, Sys. IContainer); function OnButton1Click () {for (var I = 0; I <. length; I ++) {// whether it is a type of instance if (a [I]. isInstanceOfType (g) {alert (gt. getName () + "is a" + a [I]. getName () + ". ");} else alert (gt. getName () + "is not a" + a [I]. getName () + ". ") ;}} function OnButton2Click () {for (var I = 0; I <. length; I ++) {// whether to inherit from a certain type if (gt. inheritsFrom (a [I]) {alert (gt. getName () + "inherits from" + a [I]. getName () + ". ");} else alert (gt. getName () + "does not inherit from" + a [I]. getName () + ". ") ;}} function OnButton3Click () {for (var I = 0; I <. length; I ++) {// determines whether the Type is an interface if (Type. isInterface (a [I]) {// determines whether the object has implemented the interface if (gt. implementsInterface (a [I]) {alert (gt. getName () + "implements the" + a [I]. getName () + "interface. ");} else alert (gt. getName () + "does not implement the" + a [I]. getName () + "interface. ");} else alert (a [I]. getName () + "is not an interface. ");}}

 

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.