维生素C.net
Talents come from diligence, and knowledge is gained by accumulation 天才源于勤奋,知识源于积累。
难忘的1654天
博客园  首页  新随笔  新文章  联系  管理  订阅 订阅
随笔- 220  文章- 1  评论- 1643 
【翻译】Atlas Document : Making JavaScript Easier 简化JavaScript开发
说明:对Atlas系列文章的翻译将以先前制定的翻译顺序进行,为了保持原文的味道,翻译 过程中尽量保证不做删节。为了保证可读性和连贯性,文中对一些词汇的翻译作了英文注释,由于能力有限,在翻译和学习过程中可能有疏漏和不当之处,还望大家 多多指点。总体来说这部分翻译的文章只是一个介绍和概括性的,更详细的部分您可以参考园子里其它讲解atlas的文章。

原文地址:这里
翻译:范维肖


ASP.NET Atlas使得您现在可以通过针对浏览器使用JavaScript编写华丽,交互性强的web应用程序。Atlas把类型系统扩展到了JavaScript以提供命名控件、继承、接口、枚举以及对字符串和数组所扩展的助手(helpers)。这些扩展使得您可以以一种结构化的方式来编写atlas应用程序,从而提高了可维护性,也可以更轻松的添加特性了。

在这一节里您将会学到如何使用下面的这些JavaScript Atlas扩展:
命名空间(Namespaces)
继承(Inheritance)
接口(Interface)


使用命名空间:

命名空间使得您可以分组通用的功能。下面的这个实例演示了如何使用Type.registerNamespace和.registerClass方法添加一个Person类到Demo这个命名控件

首先要添加ScriptManager到您的页面,然后注册一个命名空间,创建一个类,然后注册这个类:
Type.registerNamespace("Demo");

Demo.Person 
= function(firstName, lastName, alias) 
{
    
var _firstName = firstName;
    
var _lastName = lastName;
    
    
this.getFirstName = function() {
        
return _firstName;
    }
    
    
}
Demo.Person.registerClass('Demo.Person', 
null, Web.IDisposable);


继承:

下面这个例子中在脚本里包含了两个类:Person和从Person继承的Employee。两个类都有私有的域(fields),也都有共有的属性和方法。此外,Employee类覆写(override)了toString的实现,通过这样,我们可以使用的是基类的功能。
Type.registerNamespace("Demo");

Demo.Person 
= function(firstName, lastName, emailAddress) {
    
var _firstName = firstName;
    
var _lastName = lastName;
    
var _emailAddress = emailAddress;
    
    
this.getFirstName = function() {
        
return _firstName;
    }
    
    
    
    
this.dispose = function() {
        alert('bye ' 
+ this.getName());
    }
}
Demo.Person.registerClass('Demo.Person', 
null, Web.IDisposable);



Demo.Person.prototype.toString 
= function() {
    
return this.getName() + ' (' + this.getEmailAddress() + ')';
}

Demo.Employee 
= function(firstName, lastName, emailAddress, team, title) {
    Demo.Employee.initializeBase(
this, [firstName, lastName, emailAddress]);
    
    
var _team = team;
    
var _title = title;
    
    
this.getTeam = function() {
        
return _team;
    }
    
this.setTeam = function(team) {
        _team 
= team;
    }
    
    
}
Demo.Employee.registerClass('Demo.Employee', Demo.Person);

Demo.Employee.prototype.toString 
= function() {
    
return Demo.Employee.callBaseMethod(this, 'toString') + '\r\n' + this.getTitle() + '\r\n' + this.getTeam();
}


使用接口:

这个例子定义了基类Animal、IPet接口和实现了IPet接口的两个子类Dog和Cat,但是子类tiger没有继承这个接口。

Type.registerNamespace("Demo.Animals");

Demo.Animals.IPet 
= function() {
    
this.getFriendlyName = Function.abstractMethod;
}
Demo.Animals.IPet.registerInterface('Demo.Animals.IPet');


Demo.Animals.Animal 
= function(name) {
    
var _name = name;
    
    
this.getName = function() {
        
return _name;
    }
}
Demo.Animals.Animal.registerAbstractClass('Demo.Animals.Animal');

Demo.Animals.Animal.prototype.toStringCustom 
= function() {
    
return this.getName();
}
Demo.Animals.Animal.prototype.speak 
= Function.abstractMethod;


Demo.Animals.Pet 
= function(name, friendlyName) {
    Demo.Animals.Pet.initializeBase(
this, [name]);
    
    
var _friendlyName = friendlyName;
    
this.getFriendlyName = function() {
        
return _friendlyName;
    }
}
Demo.Animals.Pet.registerAbstractClass('Demo.Animals.Pet', Demo.Animals.Animal, Demo.Animals.IPet);


Demo.Animals.Cat 
= function(friendlyName) {
    Demo.Animals.Cat.initializeBase(
this, ['Cat', friendlyName]);
}
Demo.Animals.Cat.registerClass('Demo.Animals.Cat', Demo.Animals.Pet);

Demo.Animals.Cat.prototype.speak 
= function() {
    alert('meow');
}

Demo.Animals.Cat.prototype.toStringCustom 
= function() {
    
return 'Pet ' + Demo.Animals.Cat.callBaseMethod(this, 'toStringCustom');
}

Demo.Animals.Felix 
= function() {
    Demo.Animals.Felix.initializeBase(
this, ['Felix']);
}
Demo.Animals.Felix.registerClass('Demo.Animals.Felix', Demo.Animals.Cat);

Demo.Animals.Felix.prototype.toStringCustom 
= function() {
    
return Demo.Animals.Felix.callBaseMethod(this, 'toStringCustom') + '  its Felix!';
}


Demo.Animals.Dog 
= function(friendlyName) {
    Demo.Animals.Dog.initializeBase(
this, ['Dog', friendlyName]);
}
Demo.Animals.Dog.registerClass('Demo.Animals.Dog', Demo.Animals.Pet);

Demo.Animals.Dog.prototype.speak 
= function() {
    alert('woof');
}


Demo.Animals.Tiger 
= function() {
    Demo.Animals.Tiger.initializeBase(
this, ['Tiger']);
}
Demo.Animals.Tiger.registerClass('Demo.Animals.Tiger', Demo.Animals.Animal);

Demo.Animals.Tiger.prototype.speak 
= function() {
    alert('grrr');
}

posted on 2006-04-21 19:23 维生素C.NET 阅读(944) 评论(2)  编辑 收藏 所属分类: Training@cnblogs

发表评论
  回复  引用  查看    
2006-04-21 22:53 | Dflying Chen      
Great work!

  回复  引用    
2007-08-03 17:33 | zjysky [未注册用户]
有什么意思啊。都是代码。copy。解释一下也行啊
社区  新闻  新用户注册  刷新评论列表  

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
所属分类的其他文章:
· 关于SQL Server的两个细节
· {Clingingboy}asp.net控件开发基础
· {tmfc's .net cabin}[翻译]了解ASP.NET底层架构
· Plan KA活动的word 2007 Publish blog的模板
· viBlogging程序源代码和数据库
· 博客园新手提高计划
· 【翻译】Atlas Document : Making JavaScript Easier 简化JavaScript开发
· Wayfarer的《叩开C#之门》系列(图文并茂,推荐新手认真看一看)
· 近期post整理
· 【翻译】Atlas Documents : UpdatePanel Class
最新IT新闻:
· Pogo浏览器
· 洪磊口述:番茄花园如何捆绑流氓软件月入十万
· 2008年8月21日IT博客精选:盖茨复出?
· 微软宣布将斥资1亿美元购买Novell证书
· 携程谷歌将联合发旅游榜单
博客园新闻频道  博客园首页  社区
 



公告

view my mvp profile 看看有多少人来访问我的Blog了!
hotmail

<2006年4月>
日一二三四五六
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

与我联系

  • 发短消息

搜索

 

常用链接

  • 我的随笔
  • 我的空间
  • 我的短信
  • 我的评论
  • 更多链接
  • 我的参与
  • 我的新闻
  • 最新评论
  • 我的标签

留言簿(168)

  • 给我留言
  • 查看留言

我参与的团队

  • 北京.NET俱乐部(1/1446)
  • 烟台.NET俱乐部(0/47)
  • ASP.NET AJAX (Atlas)学习(0/1340)
  • MVP(微软最有价值专家)团队(1/540)
  • 博客园培训团队(0/110)
  • Silverlight学习与研究(0/282)
  • CLR基础研究团队(0/374)

随笔分类(148)

  • ASP.NET(26)
  • Code Warehouse(20)
  • IronRuby,DLR(2)
  • LINQ(3)
  • Reading(3)
  • Training@cnblogs(23)
  • Ubuntu(4)
  • Windows Live(6)
  • Windows Mobile(7)
  • XHTML & Web Standard(54)

随笔档案(220)

  • 2008年3月 (2)
  • 2008年1月 (3)
  • 2007年12月 (3)
  • 2007年9月 (1)
  • 2007年8月 (2)
  • 2007年7月 (3)
  • 2007年6月 (3)
  • 2007年3月 (4)
  • 2007年2月 (3)
  • 2007年1月 (1)
  • 2006年12月 (1)
  • 2006年11月 (8)
  • 2006年10月 (6)
  • 2006年9月 (11)
  • 2006年8月 (5)
  • 2006年7月 (4)
  • 2006年6月 (1)
  • 2006年5月 (10)
  • 2006年4月 (8)
  • 2006年2月 (2)
  • 2006年1月 (1)
  • 2005年12月 (11)
  • 2005年11月 (13)
  • 2005年10月 (3)
  • 2005年9月 (1)
  • 2005年8月 (4)
  • 2005年7月 (3)
  • 2005年6月 (4)
  • 2005年4月 (5)
  • 2005年3月 (10)
  • 2005年2月 (7)
  • 2005年1月 (28)
  • 2004年12月 (15)
  • 2004年11月 (10)
  • 2004年10月 (5)
  • 2004年9月 (1)
  • 2004年6月 (13)
  • 2004年5月 (5)

文章档案(1)

  • 2005年5月 (1)

相册

  • ASPNET2tutorial
  • BlogUsing
  • My love and my friends
  • newGallery
  • 下一代网络图片

.net网站收藏

  • ASP.NET2.0 Tutorial
  • CodeBetter.com
  • F#
  • IIS.net
  • MS NewsGroup
  • NewsGroups
  • OnlyVC.org
  • VWD2005GuidedTour
  • ZDNet China软件技术专区

OSS 2007

  • Charsh
  • Kaneboy
  • Official Team Blog

Python

  • BeginnersGuide

好友的BLOG

  • DemoFox@JoyCode
  • DflyingChen
  • dudu
  • EricLee
  • hbifts
  • idior
  • Jesee Qing
  • Lion
  • Rickie
  • Samuel
  • Steph`s Website
  • 翱翔.Net
  • 陈敬熙
  • 发条木偶
  • 葛涵涛
  • 古道风
  • 寒枫天伤
  • 老猫の理想
  • 刘老师
  • 刘彦博
  • 吕震宇
  • 木野狐
  • 佘广
  • 王sir
  • 小涛
  • 小新
  • 肖老师
  • 旋哥

积分与排名

  • 积分 - 393425
  • 排名 - 53

最新评论

  • 1. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --Winston
  • 2. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --Birdshover
  • 3. re: .NET Beginner Training Step by Step开始启动
  • 喜欢没有理由!
    申请加入
  • --倔强
  • 4. re: .NET Beginner Training Step by Step开始启动
  • 申请加入
  • --清道夫-WPF
  • 5. re: .NET Beginner Training Step by Step开始启动
  • 申请加入,向大家学习
  • --ten.psa

阅读排行榜

  • 1. 英文名字及含义(25073)
  • 2. SQL Server 2005 Remote Access(14891)
  • 3. Visual Studio 2005 Team Edition和SQL Server 2005的下载(14282)
  • 4. Windows Installer 3.1(11433)
  • 5. Visual Studio 2005 Professional Released(10925)

评论排行榜

  • 1. .NET Beginner Training Step by Step开始启动(317)
  • 2. Windows Live Messenger 8.0 Beta 的邀请(100)
  • 3. 加入[ 下一代网络web技术(Next Generation Web Application)团队Blog ](88)
  • 4. 博客园新手.net技术培训活动(55)
  • 5. 为什么在vista上做开发?(54)
Copyright ©2008 维生素C.NET