说明:对Atlas系列文章的翻译将以先前制定的翻译顺序进行,为了保持原文的味道,翻译
过程中尽量保证不做删节。为了保证可读性和连贯性,文中对一些词汇的翻译作了英文注释,由于能力有限,在翻译和学习过程中可能有疏漏和不当之处,还望大家
多多指点。总体来说这部分翻译的文章只是一个介绍和概括性的,更详细的部分您可以参考园子里其它讲解atlas的文章。
原文地址:这里
翻译:范维肖
ASP.NET Atlas使得您现在可以通过针对浏览器使用JavaScript编写华丽,交互性强的web应用程序。Atlas把类型系统扩展到了JavaScript以提供命名控件、继承、接口、枚举以及对字符串和数组所扩展的助手(helpers)。这些扩展使得您可以以一种结构化的方式来编写atlas应用程序,从而提高了可维护性,也可以更轻松的添加特性了。
在这一节里您将会学到如何使用下面的这些JavaScript Atlas扩展:
命名空间(Namespaces)
继承(Inheritance)
接口(Interface)
使用命名空间:
命名空间使得您可以分组通用的功能。下面的这个实例演示了如何使用Type.registerNamespace和.registerClass方法添加一个Person类到Demo这个命名控件
首先要添加ScriptManager到您的页面,然后注册一个命名空间,创建一个类,然后注册这个类:
继承:
下面这个例子中在脚本里包含了两个类:Person和从Person继承的Employee。两个类都有私有的域(fields),也都有共有的属性和方法。此外,Employee类覆写(override)了toString的实现,通过这样,我们可以使用的是基类的功能。
使用接口:
这个例子定义了基类Animal、IPet接口和实现了IPet接口的两个子类Dog和Cat,但是子类tiger没有继承这个接口。
原文地址:这里
翻译:范维肖
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;
}
![](https://www.cnblogs.com/Images/dot.gif)
}
Demo.Person.registerClass('Demo.Person', null, Web.IDisposable);
Demo.Person = function(firstName, lastName, alias)
{
var _firstName = firstName;
var _lastName = lastName;
this.getFirstName = function() {
return _firstName;
}
![](https://www.cnblogs.com/Images/dot.gif)
}
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;
}
![](https://www.cnblogs.com/Images/dot.gif)
this.dispose = function() {
alert('bye ' + this.getName());
}
}
Demo.Person.registerClass('Demo.Person', null, Web.IDisposable);
![](https://www.cnblogs.com/Images/dot.gif)
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;
}
![](https://www.cnblogs.com/Images/dot.gif)
}
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();
}
Demo.Person = function(firstName, lastName, emailAddress) {
var _firstName = firstName;
var _lastName = lastName;
var _emailAddress = emailAddress;
this.getFirstName = function() {
return _firstName;
}
![](https://www.cnblogs.com/Images/dot.gif)
this.dispose = function() {
alert('bye ' + this.getName());
}
}
Demo.Person.registerClass('Demo.Person', null, Web.IDisposable);
![](https://www.cnblogs.com/Images/dot.gif)
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;
}
![](https://www.cnblogs.com/Images/dot.gif)
}
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');
}
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') + '
![](https://www.cnblogs.com/Images/dot.gif)
}
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');
}