利用MS AJAX注册Javascript命名空间并创建类
程序员文章站
2022-05-27 17:10:40
一、为页面添加一个scriptmanager控件。 二、注册命名空间: type.registernamespace("demo"); 三、为类添加构造函数、属性、方法。...
一、为页面添加一个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>
保存后看下运行效果。
二、注册命名空间:
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>
保存后看下运行效果。
上一篇: 揭秘:吐蕃王朝是如何一步一步走向灭亡的
下一篇: 揭秘:诸葛亮是否真的完美无缺?