直入正题吧!!!
Atlas剖析:
Atlas使用了一系列ASP.NET Server Controls, Web Services 和 JavaScript libraries.
使JavaScript更简单:
Atlas对JavaScript附加了很多类型系统扩展,如namespace, 继承, 接口, 枚举 和 添加了如 String 和 Array等等助手类型(helper).简而言之,Atlas是一种扩展的JavaScript语言.
下面来看看Atlas的一些主要扩展:
Namespace
Inheritance
Interface
使用Namespace
e.g.:
Type.registerNamespace("Demo");
Demo.Person = function(firstName, lastName, alias)
{
var _firstName = firstName;
var _lastName = lastName;
this.getFirstName = function() {
return _firstName;
}
...
}
Type.registerClass('Demo.Person', null, Web.IDisposable);
Inheritance
e.g:
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());
}
}
Type.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;
}
...
}
Type.registerClass('Demo.Employee', Demo.Person);
Demo.Employee.prototype.toString = function() {
return Demo.Employee.callBaseMethod(this, 'toString') + '/r/n' + this.getTitle() + '/r/n' + this.getTeam();
Interface
e.g.:
Type.registerNamespace("Demo.Animals");
Demo.Animals.IPet = function() {
this.getFriendlyName = Function.abstractMethod;
}
Type.registerInterface('Demo.Animals.IPet');
Demo.Animals.Animal = function(name) {
var _name = name;
this.getName = function() {
return _name;
}
}
Type.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;
}
}
Type.registerAbstractClass('Demo.Animals.Pet', Demo.Animals.Animal, Demo.Animals.IPet);
Demo.Animals.Cat = function(friendlyName) {
Demo.Animals.Cat.initializeBase(this, ['Cat', friendlyName]);
}
Type.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']);
}
Type.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]);
}
Type.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']);
}
Type.registerClass('Demo.Animals.Tiger', Demo.Animals.Animal);
Demo.Animals.Tiger.prototype.speak = function() {
alert('grrr');
}