ASP.NET AJAX 创建类
<%@ Page Language="C#" %> <!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 runat="server"> <title>Classes</title> <script type="text/javascript"> function pageLoad(sender, args) { Type.registerNamespace('Wrox.AspAjax.Samples.Album'); Wrox.AspAjax.Samples.Album = function (title, artist) { this._title = title; this._artist = artist; } Wrox.AspAjax.Samples.Album.prototype = { get_title: function () { return this._title; }, get_artist: function () { return this._artist; } } Wrox.AspAjax.Samples.Album.registerClass ("Wrox.AspAjax.Samples.Album"); var anAlbum = new Wrox.AspAjax.Samples.Album("Lost Highway", "Bon Jovi"); alert(anAlbum.get_title()); alert(Type.isNamespace(Wrox.AspAjax.Samples.Album)); alert(Type.isClass(Wrox.AspAjax.Samples.Album)); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> </div> </form> </body> </html>
类型继承:
<%@ Page Language="C#" %> <!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 runat="server"> <title>Classes</title> <script type="text/javascript"> function pageLoad(sender, args) { Type.registerNamespace("Wrox.AspAjax.Samples.Album"); Wrox.AspAjax.Samples.Album = function (title, artist) { this._title = title; this._artist = artist; } Wrox.AspAjax.Samples.Album.prototype = { get_title: function () { return this._title; }, get_artist: function () { return this._artist; } } Wrox.AspAjax.Samples.Album.registerClass('Wrox.AspAjax.Samples.Album'); Wrox.AspAjax.Samples.TributeAlbum = function (title, artist, tributeArtist) { Wrox.AspAjax.Samples.TributeAlbum.initializeBase(this, [title, artist]); this._tributeArtist = tributeArtist; } Wrox.AspAjax.Samples.TributeAlbum.prototype = { get_tributeArtist: function () { return this._tributeArtist; }, set_tributeArtist: function (tributeArtist) { this._tributeArtist = tributeArtist; }, get_artist: function () { return ("TRIBUTE:" + Wrox.AspAjax.Samples.TributeAlbum.callBaseMethod(this, "get_artist")); } } Wrox.AspAjax.Samples.TributeAlbum.registerClass( 'Wrox.AspAjax.Samples.TributeAlbum', Wrox.AspAjax.Samples.Album); var anotherAlbum = new Wrox.AspAjax.Samples.TributeAlbum("We're a Happy Family", "Various Artists", "Ramones"); alert(anotherAlbum.get_title()); alert(anotherAlbum.get_tributeArtist()); var anAlbum = new Wrox.AspAjax.Samples.Album("Lost Highway", "Bon Jovi"); if (Wrox.AspAjax.Samples.TributeAlbum.isInstanceOfType(anAlbum) == false) { alert("anAlbum is not a TributelAlbum"); } if (Wrox.AspAjax.Samples.TributeAlbum.isInstanceOfType(anotherAlbum) == true) { alert("anotherAlbum is a TributeAlbum"); } if (Wrox.AspAjax.Samples.TributeAlbum.inheritsFrom(Wrox.AspAjax.Samples.Album)) { alert("TributeAlbum inherits from Album"); } if (Wrox.AspAjax.Samples.Album.inheritsFrom(Wrox.AspAjax.Samples.TributeAlbum) == false) { alert("Album does not inherit from TributeAlbum"); } var typeString = "Wrox.AspAjax.Samples.TributeAlbum"; var typeCheck = Type.parse(typeString); alert(Type.isClass(typeCheck)); alert(Type.isClass(Wrox.AspAjax.Samples.TributeAlbum)); alert(typeCheck); if (Type.isClass(typeCheck)) { alert(typeCheck.getBaseType().getName()); } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> </div> </form> </body> </html>