Atlas中的JS用法 --zt
今天看了下MS的"Altas"项目,感觉好好!的确在面对对象的编写思维方面"Altas"比"AJAX"更直接。而且从Web2.0的角度来看,这个东西也确实值得我去学习学习。呵呵,互连网的第2次革命。
单独说javascript方面的特色:官方网站是如此描述
ASP.NET "Atlas" enables you to write rich interactive applications that target the browser.
These "Atlas" applications are written in JavaScript "Atlas" adds a number of type system extensions to JavaScript to enable the usage of namespaces, inheritance, interfaces, enums and helpers for Strings and Arrays amongsts others. These extensions enable 'Atlas' applications to be written in a structured way that improve maintainability,
easier to add features and layering of functionality.
下面给出我翻译的意思(不对请指出相互学习)
asp.net "Atlas" 能够使你开发出以浏览器为目标的富有交互性的应用程序。这些"Atlas"应用程序编写于
javascript ."Atlas" 加入了一些系统扩充类型到javascript使之能够使用 namespaces,inheritance
interface,enums 并且对string 与 Arrays及其他进行了扩展。这些"Atlas"应用程序能(比过去程序写法)
更有结构性,一定程度上改进了可读性,更容易去添加新的特性和功能层次化。
首先建立一个名为Namespace.js的文件
Type.registerNamespace("Demo"); //首先注册个名字空间Demo //给名字空间添加个类Person//给定构造函数 Demo.Person = function(firstName, lastName, emailAddress) { //private 私有字段 var _firstName = firstName; var _lastName = lastName; var _emailAddress = emailAddress; //类方法 通过方法访问/设置私有字段 this.getFirstName = function() { return _firstName; } this.getLastName = function() { return _lastName; } this.getEmailAddress = function() { return _emailAddress; } this.setEmailAddress = function(emailAddress) { _emailAddress = emailAddress; } //dispose()释放 注册类Person时候说明继承接口Web.IDisposable this.dispose = function() { alert('Person.dispose() :' + this.getName()); } this.getName = function(){ return this.getFirstName() + this.getLastName(); } } //注册类 Type.registerClass('Demo.Person', null, Web.IDisposable); //实例方法Demo.Person.prototype.sendMail = function() { var emailAddress = this.getEmailAddress(); //检测邮寄 if (emailAddress.indexOf('@') < 0) { emailAddress = emailAddress + '@MSN.com'; } alert('sendMail to:' + emailAddress + ' ...');}//一定要toString()否则new 出来的实例会导致异常Demo.Person.prototype.toString = function () { return (this.getFirstName() + this.getLastName());}//类Employee继承基础类的[firstName, lastName, emailAddress]Demo.Employee = function(firstName, lastName, emailAddress, team, title) { Demo.Employee.initializeBase(this, [firstName, lastName, emailAddress]); //private 私有字段 var _team = team; var _title = title; //类方法 通过方法访问私有字段 this.getTeam = function() { return _team; } this.setTeam = function(team) { _team = team; } this.getTitle = function() { return _title; } this.setTitle = function(title) { _title = title; } this.dispose = function() { alert('Employee.dispose()' + this.getFirstName()); } }//注册类 指明Employee继承自PersonType.registerClass('Demo.Employee', Demo.Person, Web.IDisposable);//实例方法//一定要toString()否则new 出来的实例会导致异常Demo.Employee.prototype.toString = function() { //调用基类的方法 return Demo.Employee.callBaseMethod(this, 'toString') + '\r\n' + this.getTitle() + '\r\n' + this.getTeam();} |
然后新建一ASPX文件如下代码:
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>入门title> <atlas:Script ID="Script1" runat="server" Path="~/ScriptLibrary/AtlasCompat.js" Browser="Mozilla" /> <atlas:Script ID="Script2" runat="server" Path="~/ScriptLibrary/AtlasCompat.js" Browser="Firefox" /> <atlas:Script ID="Script3" runat="server" Path="~/ScriptLibrary/AtlasRuntime.js" />head><body> <div> <input id="Button1" value="测试Employee" type="button" onclick="return EmployeeClick()" /> <br> <input id="Button2" value="测试Person" type="button" onclick="return PersonClick()" /> div> <script type="text/javascript" src="Namespace.js">script> <script type="text/javascript" language="javascript"> function GetTestPerson() { return new Demo.Person('Jane', 'Doe', 'jane.doe@example.com'); } function GetTestEmployee() { return new Demo.Employee('John', 'Doe', 'john.doe@example.com', 'Platform', 'Programmer'); } function PersonClick() { var aPerson=GetTestPerson(); alert(aPerson);//等同于aPerson.toString(); aPerson.dispose();//释放 } function EmployeeClick() { var aPerson = GetTestEmployee(); aPerson.setTeam("PC Table");//设置所属小组 alert(aPerson);//等同于aPerson.toString(); aPerson.sendMail(); aPerson.dispose();//释放 } script> body> html> |
之后再IIS里去运行(请确认你已经下载了Altas相关的数据集与js文件)!程序本身很简单只不过是跳出几个对话框,但js代码却明显的是面对对象的思维方式!
而且还夹杂着net机制在里面,very good!真是太棒了!