当前位置: 移动技术网 > IT编程>开发语言>.net > 利用MS AJAX注册Javascript命名空间并创建类

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

2018年04月25日  | 移动技术网IT编程  | 我要评论

苏易正秋佳乙,黄光亮,动物趣闻

一、为页面添加一个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() {

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);



namespace.aspx代码:

<!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>

<title>namespace</title>

</head>


<body>

<form id="main" runat="server">

<asp:scriptmanager runat="server" id="scriptmanager" />

</form>


<div>

<p>this example creates an instance of the person class 

and puts it in the "demo" namespace.</p>


<input id="button1" value="create demo.person" 

type="button" onclick="return onbutton1click()" />


</div>


<script type="text/javascript" src="namespace.js"></script>

<script type="text/javascript" language="javascript">


function onbutton1click() 

{

var testperson = new demo.person( 

'john', 'smith', 'john.smith@example.com');

alert(testperson.getfirstname() + " " + 

testperson.getlastname() );


return false;

}



</script>


</body>

</html>


保存后看下运行效果。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网