利用MS AJAX注册Javascript命名空间并创建类

所有的人都想停留在那山顶,但是所有的乐趣和成长都是发生在往上爬的过程中。新的一天,早安!
一、为页面添加一个ScriptManager控件。 二、注册命名空间: Type.registerNamespace("Demo");
三、为类添加构造函数、属性、方法。 Demo.sample=function(){} 四、注册类。 Demo.Person.registerClass('Demo.sample',null,Sys.IDisposable);
下面是一个具体的实例: Namespace.js Type.registerNamespace("Demo");
Demo.Person=function(firstName,lastName,emailAddress){ this._firstName=firstName; this._lastName=lastName; this._emailAddress=emailAddress; }
Demo.Person.prototype={
getFirstName:function(){ returnthis._firstName; },
getLastName:function(){ returnthis._lastName; },
getName:function(){ returnthis._firstName+''+this._lastName; },
dispose:function(){ alert('bye'+this.getName()); } } Demo.Person.registerClass('Demo.Person',null,Sys.IDisposable); Namespace.aspx代码: <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <title>Namespace</title> </head>
<body> <formid="Main"runat="server"> <asp:ScriptManagerrunat="server"ID="scriptManager"/> </form>
<div> <p>ThisexamplecreatesaninstanceofthePersonclass andputsitinthe"Demo"namespace.</p>
<inputid="Button1"value="CreateDemo.Person" type="button"onclick="returnOnButton1Click()"/>
</div>
<scripttype="text/javascript"src="Namespace.js"></script> <scripttype="text/javascript"language="JavaScript">
functionOnButton1Click() { vartestPerson=newDemo.Person( 'John','Smith','john.smith@example.com'); alert(testPerson.getFirstName()+""+ testPerson.getLastName());
returnfalse; } </script>
</body> </html>
保存后看下运行效果。

到此这篇关于利用MS AJAX注册Javascript命名空间并创建类就介绍到这了。我深信卓越的创意作品,永远是一个成功代理商前进巨轮的中轴。更多相关利用MS AJAX注册Javascript命名空间并创建类内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

标签: MS AJAX