利用MS AJAX注冊Javascript命名空間并創(chuàng)建類
更新時間:2007年10月27日 12:06:03 作者:
一、為頁面添加一個ScriptManager控件。
二、注冊命名空間:
Type.registerNamespace("Demo");
三、為類添加構(gòu)造函數(shù)、屬性、方法。
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");
三、為類添加構(gòu)造函數(shù)、屬性、方法。
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>
保存后看下運行效果。
您可能感興趣的文章:
- Javascript使用function創(chuàng)建類的兩種方法(推薦)
- Javascript 創(chuàng)建類并動態(tài)添加屬性及方法的簡單實現(xiàn)
- JS創(chuàng)建類和對象的兩種不同方式
- JavaScript中創(chuàng)建類/對象的幾種方法總結(jié)
- JavaScript創(chuàng)建類/對象的幾種方式概述及實例
- javascript最常用與實用的創(chuàng)建類的代碼
- 討論javascript(一)工廠方式 js面象對象的定義方法
- javascript工廠方式定義對象
- JavaScript中使用構(gòu)造器創(chuàng)建對象無需new的情況說明
- JavaScript設(shè)計模式之工廠模式和構(gòu)造器模式
- javascript 混合的構(gòu)造函數(shù)和原型方式,動態(tài)原型方式
- JS常見創(chuàng)建類的方法小結(jié)【工廠方式,構(gòu)造器方式,原型方式,聯(lián)合方式等】
相關(guān)文章
ASP.NET 清除模式窗口數(shù)據(jù)緩存的操作方式
模式窗口showModalDialog()彈出頁面在asp.net中經(jīng)常用到,接下來為大家介紹下清除模式窗口緩存數(shù)據(jù)的問題2013-04-04DataGrid使用心得(調(diào)用及連接數(shù)據(jù)庫等等)
在工作中遇到把DataGrid中綁定的后臺數(shù)據(jù)庫數(shù)據(jù)展示給用戶時把負數(shù)變?yōu)?的小問題,現(xiàn)在把它記錄下來包括DataGrid的調(diào)用/連接數(shù)據(jù)庫進行操作等等,感興趣的朋友可以了解下,或許本新得對你有所幫助2013-02-02ASP.NET Core如何添加統(tǒng)一模型驗證處理機制詳解
這篇文章主要給大家介紹了關(guān)于ASP.NET Core如何添加統(tǒng)一模型驗證處理機制的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2018-05-05關(guān)于ASP.NET中TreeView用法的一個小例子
下面是一個Treeview動態(tài)的綁定3層深度的樹的代碼,有需要的朋友可以參考一下2013-12-12ASP.NET之Response.Cookies.Remove 無法刪除COOKIE的原因
在web開發(fā)中Cookie是必不可少的,.NET自然也有一個強大的Cookie操作類,我們用起來也非常方便,不過在使用中我們會發(fā)現(xiàn)一個坑爹的事情Response.Cookies.Remove刪除不了Cookie。2013-06-06asp.net Repeater 數(shù)據(jù)綁定的具體實現(xiàn)(圖文詳解)
此例子綁定的數(shù)據(jù)源為微軟在mssql2000中提供的Northwind數(shù)據(jù)庫中的表Categories。2013-07-07