`
zhanshenlvbu
  • 浏览: 109320 次
社区版块
存档分类
最新评论

ActionScript中的is类型和as类型

    博客分类:
  • Flex
阅读更多

is 运算符
        is 运算符是 ActionScript 3.0 中的新增运算符,它可用来测试变量或表达式是否为给定数据类型的成员。在早期的 ActionScript 版本中,此功能由 instanceof 运算符提供。但在ActionScript 3.0 中,不应使用 instanceof 运算符来测试变量或表达式是否为数据类型的成员。对于手动类型检查,应用 is 运算符来代替 instanceof 运算符,因为表达式 x
instanceof y 只是在 x 的原型链中检查 y 是否存在(在 ActionScript 3.0 中,原型链不能全面地描述继承层次结构)。


      is 运算符检查正确的继承层次结构,不但可以用来检查对象是否为特定类的实例,而且还可以检查对象是否是用来实现特定接口的类的实例。下面的示例创建 Sprite 类的一个名为mySprite 的实例,并使用 is 运算符来测试 mySprite 是否为 Sprite 和 DisplayObject 类的实例,以及它是否实现 IEventDispatcher 接口:

var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite); // true
trace(mySprite is DisplayObject); // true
trace(mySprite is IEventDispatcher); // true

 

 

 

        is 运算符检查继承层次结构,并正确地报告 mySprite 与 Sprite 和 DisplayObject 类兼容(Sprite 类是 DisplayObject 类的子类) 。is 运算符还检查 mySprite 是否是从实现IEventDispatcher 接口的任意类继承的。由于 Sprite 类是从实现 IEventDispatcher 接口的 EventDispatcher 类继承的,因此 is 运算符会正确地报告 mySprite 也实现该接口。
        下面的示例说明与上一个示例相同的测试,但使用的是 instanceof 运算符,而不是 is 运算符。instanceof 运算符正确地识别出 mySprite 是 Sprite 或 DisplayObject 的实例,但是,当它用来测试 mySprite 是否实现 IEventDispatcher 接口时,返回的却是 false。

trace(mySprite instanceof Sprite); // true
trace(mySprite instanceof DisplayObject); // true
trace(mySprite instanceof IEventDispatcher); // false

  


as 运算符
     as 运算符是 ActionScript 3.0 中的新增运算符,也可用来检查表达式是否为给定数据类型的成员。但是,与 is 运算符不同的是,as 运算符不返回布尔值,而是返回表达式的值(代替 true)或 null (代替 false)。下面的示例说明了在简单情况下使用 as 运算符替代 is运算符的结果,例如,检查 Sprite 实例是否为 DisplayObject、IEventDispatcher 和Number 数据类型的成员。

var mySprite:Sprite = new Sprite();
trace(mySprite as Sprite); // [Sprite 对象]
trace(mySprite as DisplayObject); // [Sprite 对象]
trace(mySprite as IEventDispatcher); // [Sprite 对象]
trace(mySprite as Number); // null

 

 

     在使用 as 运算符时,右侧的操作数必须是数据类型。如果尝试使用表达式(而非数据类型)作为右侧的操作数,将会产生错误。

分享到:
评论

相关推荐

    ActionScript 3 For Adobe Flash CS4 Professional

    This guide is intended as a quick guide and reference that complements the more extensive migration resources and documentation available on the ActionScript 3 Technology Center: adobe....

    ActionScript3.0

    It is intended to be used with other instructional media, such as the Programming ActionScript 3.0 guide, and resources on the Adobe website, such as the ActionScript Topic Center.

    AdvancED ActionScript 3.0: Design Patterns

    Whether it is an example you can utilize as-is, or one you can start with and develop further, you will have a glossary of definitions and organizational concepts at your fingertips. Object-oriented...

    Foundation ActionScript 3.0 with Flash CS3 and Flex

    ActionScript is now a full-fledged programming language, with complete object-oriented capabilities, improved event handling, sound and video support, drawing capabilities, support for regular ...

    开源as3基础类库as3isolib

    开源as3基础类库as3isolib as3isolib is a great isometric library for actionscript 3 by Justin Opitz. This is a lower level isometric library that could be used in

    FLEX Flash 连接 MySQL有源码 ver2.8最新版

    Actionscript 3 MySql Driver asSQL is an Actionscript 3 Mysql Driver aimed towards AIR projects to allow Mysql database connectivity directly from Actionscript. flash直接连mysql组件,有源码。

    AS3.0高级设计模式

    be reinforcing your knowledge up to that point, as well as creating a foundation for your understanding of the design pattern chapters. Chapters 6-8 thoroughly cover design patterns. Each pattern ...

    flash_as3_programming

    This manual provides a foundation for developing applications in ActionScript™ 3.0. To best understand the ideas and ...knowledge of ActionScript 1.0 or ActionScript 2.0 is helpful but not necessary.

    xpath-as3-1.0.1

    xpath-as3-1.0 is use for paser html dom with actionscript language

    (FLEX)Advanced ActionScript 3 with Design Patterns - 2006 (Adobe)

    This is a document about (FLEX)Advanced ActionScript 3 with Design Patterns - 2006 (Adobe). It teaches us how to write AS code with design pattern. It's good for the flex developer.

    as3.0殿堂之路代码

    as3.0殿堂之路代码 I coded it word by word. It is not copycat. I hope it can help the person who want learn as . Thanks for Download.

    cocoaAMF使用指南.zip

    AMF使用HTTP方式传输,目前主要是用于ActionScript中,即实现Flex和Server之间的通信。 For sending a remoting call see SimpleRemotingCallExample. For setting up a server see ServerExample. For testing an ...

    超简单as脚本解释器代码.rar

    ATA is atlantis's script 大概13年上半年做页游时候,ActionScript3不方便弄脚本,突发奇想自己写个脚本来跑,按照简单的C语言语法,看了下编译原理,手写递归下降,实现了一个很简单的解释器,懒了运行时没做栈执行,后来...

    Python SWFTools

    Includes support for both ActionScript 2.0 as well as ActionScript 3.0. • SWFExtract Allows to extract Movieclips, Sounds, Images etc. from SWF files. • AS3Compile A standalone ActionScript 3.0 ...

    flash相册,国外免费相册

    * No actionscript experience is needed, * The picture viewer has standard sizes and you don't have to resize your images. RXP does this for you, so every image in gallery will be adapted to viewer ...

    Adobe Flex 4.5 Fundamentals

    Written by a team of authors with practical experience as consultants, mentors and developers of courseware, this book/CD uses project-based tutorials, and is designed to teach beginning Flex ...

    首页滚动新闻栏目下载

     - include 读入外部的ActionScript程序文件(.as)  - loadMovie 引入MC  - loadVariables 引入外部文件中的变量值  - on 鼠标事件的触发条件  - onClipEvent MC的事件触发程序  - play 播放

    flash as3.0 programming 英文版

    The Adobe Developer Center is your resource for up-to-the-minute information on ActionScript, articles about real-world application development, and information about important emerging issues. View ...

    Adobe flex3.pdf

    progAS_flex3.pdf.This manual provides a foundation for developing applications in ActionScript™ 3.0.... Prior knowledge of ActionScript 1.0 or ActionScript 2.0 is helpful but not necessary.

    The Essential Guide To Open Source Flash Development

    Nicolas is the author of several popular open source tools, including the MTASC open source ActionScript2 compiler, the haXe crossplatform compiler and the Neko Virtual Machine. He is cofounder and ...

Global site tag (gtag.js) - Google Analytics