ActionScript 3或许是现在最接近
ECMAScript Edition 4的实现了.其中也包含了对
ECMAScript for XML (E4X)的实现(之前最早实现对E4X支持的应该是BEA Weblogic Workshop吧).
Professional JavaScript for Web Developers里, 20.2 ECMAScript for XML里对E4X的描述有点过时了.至少20.2.1里开头的好几个例子都不符合于现在的规范了(E4X 2nd).而当前AS3对E4X的实现应该是对的吧...我还没时间仔细把E4X的规范通读.
下面稍微展现几个有趣的地方,例如说新加入的获取/修改XML节点内容的方法, ".."运算符, for each循环等.要写得更详细那就变教程了,我只想在这里展示些有趣的东西而已.
var node : XML = <books>
<book type="paperback">
Author1, Book 1
<!--
<author>Author1</author>
<name>Book 1</name>
-->
</book>
<book type="hardcover">
<author>Author2</author>
<name>Book 2</name>
</book>
</books>;
var typeOfBook1 : String = node.book[0].@type; // fetch the type of the first <book>
// node.["book"][0].@["type"] will work too.
// this gets around keyword conflict problems
print( typeOfBook1 );
// Output:
// paperback
print( );
var nameOfBook2 : String = node.book[1].name; // fetch the <name> of the second <book>
print( nameOfBook2 );
// Output:
// Book 2
print( );
var list : XMLList = node..book; // fetch the list of <book> nodes from the given node
for ( var index in list ) {
print( index );
}
// Output:
// 0
// 1
print( );
for each ( var val in list ) {
print( val );
}
// Output:
// Author1, Book 1
// <book type="hardcover">
// <author>Author2</author>
// <name>Book 2</name>
// </book>
list = <>
<a>Something</a>
<a>Something else</a>
</>; // XMLList literal
list[0] = "Something first"; // same as <a>Something first</a> here
for each ( var value in list ) {
print( value );
}
// Output:
// Something first
// Something else
var node : XML = <books return="mi">
<book type="paperback">
Author1, Book 1
</book>
</books>;
print( node.@["return"] ); // get around AS3 keywords with alternative syntax
这些新玩具比XPath看起来亲切多了 ...T T
不过要用XLST的话还是得靠XPath ...T T
var process : XML = <process>
</process>
var state1 : XML = <state></state>
state1.@g = '123,456'
state1.@name = 'my name1'
process.appendChild( state1 )
var state2 : XML = <state></state>
state2.@g = '12344'
state2.@name = 'my name2'
process.appendChild( state2 )
print(process)
// Output:
// <process>
// <state g="123,456" name="my name1"/>
// <state g="12344" name="my name2"/>
// </process>
var books : XML = <books xmlns="abcdefgh">
<book>
<name>Book Title 1</name>
</book>
<book>
<name>Book Title 2</name>
</book>
</books>
var bookNS : Namespace = new Namespace('abcdefgh')
print(books.bookNS::book)
print()
print(books.bookNS::book.bookNS::name[0])
// Outputs:
// <book xmlns="abcdefgh">
// <name>Book Title 1</name>
// </book>
// <book xmlns="abcdefgh">
// <name>Book Title 2</name>
// </book>
//
// Book Title 1
传闻Java SE 7也有在语言级别支持XML的可能,不知道如果真的实现的话,是否也会跟E4X很像呢?
话说回来,ES4里新增加的内容多得真够戗.于是又有很多人在抱怨"kitchen-sink"问题了;不,更多的是说"over engineered".原本的JS1/ES1/ES2/ES3确实是比不少语言简单,但真要说"很简单"也未必.光是有first-class function就已经有不少复杂度了;对不习惯prototype based OO的人来说,只有原型而没有"类"或许也是难以理解的地方.
新的ES4的话...嗯嗯,不好说.《人月神话》里有个经典的说法: "the second system syndrome",这在ES4上真是...
ES4的overview里有这么一句话:
引用
The reason for introducing type annotations into ECMAScript is their benefit to programming in the large, not their potential benefits to performance.
虽说"不只是为了性能",但性能不得不说是其中的重要考虑因素.之前我还跟师兄开玩笑,说要是照这种势头,既然变量是"为了提高运行效率"而可以指定类型,那容器类最好也能指定其内容的类型,然后我们就有...泛型容器了.事实上,ES4里我们可以写:
Vector.<int>
眼熟么? ...用过C++/Java SE 5 or later/C# 2.0 or later的人肯定会觉得眼熟.
C++:
vector<int>
Java:
ArrayList<Integer>
C#:
List<int>
也难怪有人说ES4几乎包含了这几十年来OO发展中出现的所有东西...
我对ES4还是抱有不少憧憬了,希望它能活下去吧.ES4只是来得太迟了而已,说不定要是再早一点就能赶上Ajax的趟了?
回头留意下InfoQ上的一篇报导:
Is the future of JavaScript ECMAScript 4?
分享到:
相关推荐
ActionScript3中文手册
本书在讲解过程中对于ActionScript 3中相对于ActionScript 2 的改动进行了一一指出,并从理论上解释了为何改变及相关的实现。使ActionScript 2 程序员能够快速上手ActionScript 3 ,并对ActionScript 2代码移植有...
ActionScript3中文教程,详细介绍了用 FLASH CS3开发应用程序。
ActionScript 3.0 是一种强大的面向对象编程语言,它标志着 Flash Player Runtime 演化过程中的一个重要阶段。设计 ActionScript 3.0 的意图是创建一种适合快速地构建效果丰富的互联网应用程序的语言,这种应用程序...
ActionScript3代码规范 ActionScript3代码规范 ActionScript3代码规范
ActionScript3小游戏-贪吃蛇 很有用的一个游戏
《Flash ActionScript3殿堂之路》共分5个部分。第1部分:ActionScript 3语言基础;第2部分:ActionScript 3面向对象编程;第3部分:ActionScript 3 核心类;第4部分:ActionScript 3主要的Flash Player API;第5部分...
ActionScript3中的JSON编码解码包
flash actionscript3游戏开发教程小as3 demo 1个人投掷西瓜把另1个人砸死.zip
聊天室(ActionScript 3.0实现)
程序中实现了交互性、数据处理以及其它许多功能。 ActionScript 是由 Flash Player 中的 ActionScript 虚拟机 (AVM) 来执行的。ActionScript 代码通常被编译器编译成“字节码格式”(一种由计算机编写且能够为计算机...
该控件用Flash CS4+ActionScript3写成,能在网页上方便调用,用FlashVars参数传入图片路径。 该控件涉及了很多AS3画图的知识点。实现了加载图片,过度平滑,能通过调整SliderBar对加载的相片随意放大缩小等功能。对...
5)ActionScript3_cookBook_en.chm Adobe Flex4.0 ActionScript3 中文API 语言参考.part1.rar的下载地址: http://download.csdn.net/source/3245173 Adobe Flex4.0 ActionScript3 中文API 语言参考.part2.rar的...
利用ActionScript实现Flash开发游戏.pdf
聊天室(ActionScript 3.0实现)
ActionScript 3 For Adobe Flash CS4 Professional About this guide This guide provides a quick introduction to migrating to ActionScript 3 from ActionScript 2. It is targeted at designers and ...
基于ActionScript 3.0实现本地播放器
ActionScript 3.0 语言和组件参考概述Adobe Flex 2 语言参考ActionScript 3.0 语言和组件参考是适用于 Flash® Player 应用程序编程接口 (API) 的参考手册。 Adobe Flex 2 语言参考ActionScript 3.0 语言和组件...
Actionscript3 中文 英文版
ActionScript语句中文全解