您的位置:首页 > 其它

Flex之旅:第一部分:flex必备基础知识积累(3)---is as typeof in instanceof 用法介绍

2015-03-11 15:10 911 查看
is运算符

is运算符是ActionScript 3.0中的新增运算符,
用来测试变量或表达式是否为给定的数据类型。返回值是true 和 false。
is运算符可以检查正确的继承层次结构,不但能够检查对象是否为特定类的实例,而且还可以检查对象是否是用来实现特定接口的类的实例。
早期版本中的instanceof运算符已不再推荐使用


代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				trace(bb is FlexSprite); // true   
				trace(bb is DisplayObject); // true   
				trace(bb is IEventDispatcher); // true  
			}
			
		]]>
	</fx:Script>
	
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:Button id="bb" >
		
	</s:Button>
</s:Application>


说明:

这里用的是spark.components.Button extends FlexSprite
FlexSprite extends DisplayObject

DisplayObject extends EventDispatcher

EventDispatcher implements IEventDispatcher 接口

总结:

可见is运算符不但可以检测继承的层级结构,
还可以检测借口实现的层级结构。
早期版本中的instanceof运算符虽然能识别出,button(bb)是FlexSprite或DisplayObject的实例,但不能识别mySprite是否实现了IEventDispatcher接口。

as运算符

as运算符是ActionScript 3.0中的新增运算符,
也可以用来测试变量是否为给定的数据类型。但是返回值不再是布尔值。
而是返回具体的对象实例(代替true)或null(代替false)。

代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				trace(bb as Sprite); // true   
				trace(bb as DisplayObject); // true   
				trace(bb as IEventDispatcher); // true  
				trace(bb as Number); // true  
			}
			
		]]>
	</fx:Script>
	
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<s:Button id="bb" >
	</s:Button>
</s:Application>


console:

TypeTest.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.bb

TypeTest.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.bb

TypeTest.ApplicationSkin2._ApplicationSkin_Group1.contentGroup.bb

null

总结:

as运算符,左侧是对象实例 右侧是对象类型
返回值,要么是类型转换后的实例,要么是null。

typeof运算符

typeof运算符的用法是: typeof expression
返回值是字符串,即以字符串的形式返回其数据类型
但是数据类型仅仅局限在6种:boolean、function、number、object、string、xml
自定义的类,在应用了此运算符之后,返回的字符串是“object”

来自官方的表格说明对各个类型的表达式使用 typeof 运算符的结果

[thead]
[/thead]

表达式类型

结果

Array

object


Boolean

boolean


Function

function


int

number


Number

number


object

object


字符串

string


uint

number


XML

xml


XMLList

xml


*

undefined


详见:http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/operators.html#typeof

in运算符

首先in运算符的返回值是布尔类型
运算符的用法是: expression1 in expression2
expression1 是String类型的值

expression2 是一个对象是实例
如果expression2 是一个对象的类型,好像一直返回false

示例代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			import vo.Persion;
			
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				var p:Persion = new Persion();
				
				trace("id" in p);	 	// true
				trace("ass" in p)		// false
				trace("id" in Persion)	        // false
				
				trace(p.hasOwnProperty("id"));	//true
				trace(p.hasOwnProperty("ass")); //false
			}
			
		]]>
	</fx:Script>
	

</s:Application>


Persion.as

package vo
{
	[Bindable]
	public class Persion
	{
		public var id:String;
		public var age:int;
		public var name:String;
		
		public function Persion()
		{
		}
	}
}


如果指定的对象是Array对象,则可以使用in运算符来检查特定的索引编号是否有效。
如果向expression1传递整数,那么,当索引位于有效的索引编号范围内时,结果为true,否则为false。
但是一个Array对象,存的是一组自定义Object,那么in运算符就不支持,从Array对象中,检索自定义Object的属性。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
			   xmlns:s="library://ns.adobe.com/flex/spark"
			   xmlns:mx="library://ns.adobe.com/flex/mx"
			   minWidth="955" minHeight="600"
			   creationComplete="application1_creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			
			import vo.Persion;
			
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				var myArray:Array = ["a", "b", "c"];   
				trace(0 in myArray); // 0 是 myArray 对象中的有效索引编号,返回true   
				trace(1 in myArray); // 1 是 myArray 对象中的有效索引编号,返回true   
				trace(2 in myArray); // 2 是 myArray 对象中的有效索引编号,返回true   
				trace(3 in myArray); // 3 不是 myArray 对象中的有效索引编号,返回false
				
				var items:Array = [  
					{label:'Live', value1:'N',refCodeA:'LIVE'},  
					{label:'Trading Hierarchy', value1:'N',refCodeA:'TRAD'},  
					{label:'Production Demo', value1:'Y',refCodeA:'PROD'},  
					{label:'PAT Demo', value1:'Y',refCodeA:'PAT'},  
					{label:'Derivative Clearance Demo', value1:'Y',refCodeA:'DCT'},  
					{label:'Client On-Boarding Tool', value1:'N',refCodeA:'CONB'}  
				];  
				trace("-----------------");
				trace("label" in items);
				trace("value1" in items);
				trace("refCodeA" in items);
				trace(0 in items);
				
			}
			
		]]>
	</fx:Script>
</s:Application>


console:

true

true

true

false

-----------------

false

false

false

true

instanceof运算符

请参看is运算符的介绍。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐