利用MS AJAX注册Javascript命名空间并创建类
所属分类:
网络编程 / ASP.NET
阅读数:
1085
收藏 0赞 0分享
一、为页面添加一个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>
保存后看下运行效果。
ADO.NET实用经验汇总
这篇文章主要介绍了ADO.NET实用经验汇总,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
DataReader不能使用using的详细示例
这篇文章主要介绍了DataReader不能使用using的详细示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
.NET连接池的问题详解
这篇文章主要介绍了.NET连接池的问题详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
.NET Core3.1发布(翻译)
这篇文章主要介绍了.NET Core3.1发布(翻译),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
浅谈ASP.NET Core的几种托管方式
这篇文章主要介绍了浅谈ASP.NET Core的几种托管方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0赞 0分享
查看更多