E4X 的另类应用
一直以来我们在 actionscript 中内嵌大段的多行文本时必须在每一号加上引号,然后再在结尾写加号。问题是一天三遍的写,太麻烦了。。。一直也没见过什么解决办法(除非写个外部文件载入,不过那个不爽,请忽略)。直到有一天。。。也就是昨天晚上,我一不小心想到了一个解决方案,大家请看:
1 2 3 4 5 6 7 8 9 10 11 12 13 | var t:String=plain()(<!-- The Flash Player API classes are in the flash.* packages. The Flash Player API refers to all packages, classes, functions, properties, constants, events, and errors that are in the flash package. They are unique to Flash Player, as opposed to the top-level classes, such as Date, Math, and XML, or the language elements, which are based on ECMAScript. The Flash Player API contains features that you expect to find in object-oriented programming languages, such as the flash.geom package for geometry classes, as well as features specific to the needs of rich Internet applications, such as the flash.filters package for expressiveness and the flash.net package for handling data transmission to and from a server. -->); |
这个 plain 方法把代码中的一整段文本复给 t 变量,而字符串本身不用加引号!关键点就在 标识符,挖哈哈,这个 e4x 的应用还不错吧。
另外一个想到的应用是不用 Flex 的 embed 标签也可以内嵌图片等二进制元素到源码中,因为 Flex 的 embed 语法是不兼容 flash9 的,而且需要莫名其妙的引入 mx 包下的一些类。所以这个应用也是有一定用处地:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var b:String=plain(/W*/g)(<!-- 89504e470d0a1a0a0000000d49484452 0000000e000000100803000000114abe 080000000467414d410000afc837058a e90000001974455874536f6674776172 650041646f626520496d616765526561 647971c9653c0000000f504c5445ffff ffff0000cccccc9999990000003952e0 8a000000314944415478da626046010c cc0c08c0c4423a9791118261b28c8c20 1e4231988743165d2fb9ce6042022c0c 2c280020c0006dc501097dfe85550000 000049454e44ae426082 -->); |
至于这一大堆乱码是什么意思,大家意会吧。。。
plain 方法的具体实现就不说了,很简单。有兴趣的朋友可以 在这里下载源码和实例 研究一蛤。
另外通过这个方法,我还发现了 as3 的编译器,以及 flex builder 2 的 as3 编辑器在词法分析上还是存在问题的。比如有时候我在 之间加入一整段类/包定义,编译器居然报错了。。。而且编辑器也很勤劳的加上语法高亮什么的。。。
