利用MS AJAX註冊Javascript命名空間並建立類

來源:互聯網
上載者:User

一、為頁面添加一個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>

儲存後看下運行效果。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.