利用MS AJAX注冊(cè)Javascript命名空間并創(chuàng)建類(lèi)
更新時(shí)間:2007年10月27日 12:06:03 作者:
一、為頁(yè)面添加一個(gè)ScriptManager控件。
二、注冊(cè)命名空間:
Type.registerNamespace("Demo");
三、為類(lèi)添加構(gòu)造函數(shù)、屬性、方法。
Demo.sample=function(){}
四、注冊(cè)類(lèi)。
Demo.Person.registerClass('Demo.sample ', null, Sys.IDisposable);
下面是一個(gè)具體的實(shí)例:
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>
保存后看下運(yùn)行效果。
二、注冊(cè)命名空間:
Type.registerNamespace("Demo");
三、為類(lèi)添加構(gòu)造函數(shù)、屬性、方法。
Demo.sample=function(){}
四、注冊(cè)類(lèi)。
Demo.Person.registerClass('Demo.sample ', null, Sys.IDisposable);
下面是一個(gè)具體的實(shí)例:
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>
保存后看下運(yùn)行效果。
您可能感興趣的文章:
- Javascript使用function創(chuàng)建類(lèi)的兩種方法(推薦)
- Javascript 創(chuàng)建類(lèi)并動(dòng)態(tài)添加屬性及方法的簡(jiǎn)單實(shí)現(xiàn)
- JS創(chuàng)建類(lèi)和對(duì)象的兩種不同方式
- JavaScript中創(chuàng)建類(lèi)/對(duì)象的幾種方法總結(jié)
- JavaScript創(chuàng)建類(lèi)/對(duì)象的幾種方式概述及實(shí)例
- javascript最常用與實(shí)用的創(chuàng)建類(lèi)的代碼
- 討論javascript(一)工廠方式 js面象對(duì)象的定義方法
- javascript工廠方式定義對(duì)象
- JavaScript中使用構(gòu)造器創(chuàng)建對(duì)象無(wú)需new的情況說(shuō)明
- JavaScript設(shè)計(jì)模式之工廠模式和構(gòu)造器模式
- javascript 混合的構(gòu)造函數(shù)和原型方式,動(dòng)態(tài)原型方式
- JS常見(jiàn)創(chuàng)建類(lèi)的方法小結(jié)【工廠方式,構(gòu)造器方式,原型方式,聯(lián)合方式等】
相關(guān)文章
基于.net standard 的動(dòng)態(tài)編譯實(shí)現(xiàn)代碼
這篇文章主要介紹了基于.net standard 的動(dòng)態(tài)編譯實(shí)現(xiàn)代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
.NET的Ajax請(qǐng)求數(shù)據(jù)提交實(shí)例
這篇文章主要介紹了.NET的Ajax請(qǐng)求數(shù)據(jù)提交實(shí)例,較為詳細(xì)的分析了Ajax請(qǐng)求、數(shù)據(jù)的提交以及參數(shù)的傳遞技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01
.NET Core3.0創(chuàng)建Worker Services的實(shí)現(xiàn)
這篇文章主要介紹了.NET Core3.0創(chuàng)建Worker Services的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Visual Studio 2017設(shè)置版權(quán)的方法
這篇文章主要為大家詳細(xì)介紹了Visual Studio 2017設(shè)置版權(quán)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
SignalR Self Host+MVC等多端消息推送服務(wù)(一)
這篇文章主要為大家詳細(xì)介紹了SignalR Self Host+MVC等多端消息推送服務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
asp.net(c#)兩種隨機(jī)數(shù)的算法,可用抽考題
asp.net(c#)兩種隨機(jī)數(shù)的算法,可用抽考題...2007-04-04
.Net Core中自定義認(rèn)證實(shí)現(xiàn)
本文主要介紹了.Net Core中自定義認(rèn)證實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
用ASP.Net實(shí)現(xiàn)文件的在線壓縮和解壓縮
用ASP.Net實(shí)現(xiàn)文件的在線壓縮和解壓縮...2006-09-09

