使用 Microsoft Ajax Library 建立自訂用戶端指令碼

來源:互聯網
上載者:User
文章目錄
  • 使用介面
  • 使用枚舉
  • 反射的用法

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

Microsoft Ajax Library是微軟提供的一套基於用戶端的Ajax js庫,通過在頁面中添加ScriptManager控制項可以管理這些指令碼和任何自訂的指令碼。

本節所介紹的所有內容均依賴於該JS庫,與伺服器開發無關!

MS Ajax Library有一些功能:

  • 向js中添加了物件導向的功能,可以使用類、命名空間、繼承等組織js代碼。
  • 反射功能,在運行時檢查客戶指令碼的結構和組件。
  • 枚舉
  • 擴充了JS的基底類型,縮短開發時間
  • 更好的調試和跟蹤功能。
JS物件導向的用法

Type 類為 JavaScript 編程添加了命名空間、類和繼承等物件導向的功能。任何使用 Type 類註冊的 JavaScript 對象都會自動獲得訪問此功能的許可權。

//註冊命名空間Type.registerNamespace("Demo");//為命名空間添加Person類,後面的方法為Person的建構函式Demo.Person = function (firstName, lastName, emailAddress) {    this._firstName = firstName;    this._lastName = lastName;    this._emailAddress = emailAddress;}//Person類的方法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.PersonDemo.Person.registerClass('Demo.Person', null, Sys.IDisposable);//定義Employee類Demo.Employee = function (firstName, lastName, emailAddress, team, title) {    Demo.Employee.initializeBase(this, [firstName, lastName, emailAddress]);    this._team = team;    this._title = title;}//Employee類的方法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, 'toString') + '\r\n' + this.getTitle() + '\r\n' + this.getTeam();    }}//註冊Demo.Employee類,並指定其繼承自Demo.Person類Demo.Employee.registerClass('Demo.Employee', Demo.Person);

 

調用的代碼:

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());
使用介面
//註冊介面Demo.Trees.IFruitTree.registerInterface('Demo.Trees.IFruitTree');
//使用介面//類Demo.Trees.FruitTree繼承了Demo.Trees.Tree,並實現了介面Demo.Trees.IFruitTreeDemo.Trees.FruitTree.registerClass('Demo.Trees.FruitTree', Demo.Trees.Tree, Demo.Trees.IFruitTree);
使用枚舉
//註冊命名空間Type.registerNamespace("Demo");//定義枚舉類型Demo.Color = function () { };Demo.Color.prototype ={    Red: 0xFF0000,    Blue: 0x0000FF,    Green: 0x00FF00,    White: 0xFFFFFF}//註冊枚舉類型Demo.Color.registerEnum("Demo.Color");//用法var color = Demo.Color.Red;

 

反射的用法

反射是指在運行時檢查程式的結構和組件的能力。實現反射的 API 是對 Type 類的擴充。通過這些方法,可以收集有關對象的資訊,例如該對象繼承自誰,它是否實現特定的介面,以及它是否是特定類的執行個體等。

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 < a.length; i++) {        //是否為某一類型的執行個體        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 < a.length; i++) {        //是否繼承自某一類型        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 < a.length; i++) {        //判斷類型是否為介面        if (Type.isInterface(a[i])) {            //判斷對象是否實現了介面            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.");    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.