利用MS AJAX注册Javascript命名空间并创建类
2007-10-27 12:06
621 查看
一、为页面添加一个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>
保存后看下运行效果。
您可能感兴趣的文章:
- Javascript使用function创建类的两种方法(推荐)
- Javascript 创建类并动态添加属性及方法的简单实现
- JS创建类和对象的两种不同方式
- JavaScript中创建类/对象的几种方法总结
- JavaScript创建类/对象的几种方式概述及实例
- javascript最常用与实用的创建类的代码
- 讨论javascript(一)工厂方式 js面象对象的定义方法
- javascript工厂方式定义对象
- JavaScript中使用构造器创建对象无需new的情况说明
- JavaScript设计模式之工厂模式和构造器模式
- javascript 混合的构造函数和原型方式,动态原型方式
- JS常见创建类的方法小结【工厂方式,构造器方式,原型方式,联合方式等】
相关文章推荐
- 利用MS AJAX注册Javascript命名空间并创建类
- 在JavaScript中创建命名空间的几种写法
- Javascript通过匿名包装器创建命名空间
- 在JavaScript中创建命名空间的几种写法
- JavaScript创建命名空间、类及类成员
- 在JavaScript中创建命名空间的几种写法
- JavaScript注册命名空间
- 【JavaScript】创建命名空间,Class,LOG
- 在JavaScript中创建命名空间的几种写法
- 在JavaScript中创建命名空间的几种写法
- JavaScript中创建命名空间的几种写法(推荐)
- JavaScript创建命名空间(namespace)的最简实现
- JavaScript创建命名空间的5种写法
- JavaScript 匿名函数、模块模式、闭包、命名空间、创建构造器(类)、继承
- JavaScript 匿名函数、模块模式、闭包、命名空间、创建构造器(类)、继承
- javascript的对象创建模式---命名空间模式
- 在JavaScript中创建命名空间的几种写法
- 在JavaScript中创建命名空间的几种写法
- Javascript通过匿名包装器创建命名空间
- 在JavaScript中创建命名空间的几种写法