【翻譯】Atlas Document : Making JavaScript Easier 簡化JavaScript開發

來源:互聯網
上載者:User
說明:對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');
}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.