基础知识this[String]

package
{
    import flash.display.Sprite;
    public dynamic class StringTest extends Sprite
    {
        public 
var  testName:String;
        public 
function  StringTest(str:String)
        {
            testName
= str;
        }
        public 
function  functionA(): void
        {
            trace(
" functionA is here! " )
        }
        public 
function  functionB(): void
        {
            trace(
" functionB is here! " )
        }
    }
}
this [ " rect " =   new  Rect();
// rect.x = 450 //会提示错误,未定义的属性
this .rect.x = this .rect.y = 100 // 没问题
this .rect.name = " rect1 " ;
addChild(
this [ " rect " ]);

this [ " rect " =   new  Rect();
this .rect.x = this .rect.y = 200 ;
this .rect.name = " rect2 " ;
addChild(
this [ " rect " ]);

var  rect:Rect = this .getChildByName( " rect1 " ) as Rect;
trace(rect.x)


var  i: int = 10 ;
var  j: int ;
while (j = i -- )
{
    trace(
" i+j= " + String(i + j))
}

var  stringTest:StringTest = new  StringTest( " test_1 " );
addChild(stringTest);
stringTest[getFunctionName()]();
trace(stringTest[
" testName " ]);
stringTest[
" rect " ] = rect;
stringTest.addChild(rect);
function  getFunctionName():String
{
    
var  t:Number = getTimer();
    
if (t % 2 == 0 ) return   " functionA " ;
    
else   return   " functionB " ;
}
import flash.utils.Dictionary; 
         
var  a:Object  =   new  Object(); 
         
var  b:Object  =   new  Object(); 
         
var  dict:Dictionary  =   new  Dictionary(); 
         dict[a] 
=   1 ;                                    //  dict[a] = 1; 
         dict[b]  =   2 ;                                     //  dict[b] = 2; 
          for  ( var  prop: *   in  dict) { 
              trace(prop);                              
//  traces: [object Object], [object Object] 
              trace(dict[prop]);                       //  traces: 1, 2 
         }

注意:

 1、虽然在trace的时候,输出的还是[object Object],但是这个结果是对象的toString的结果。在Dictionary对象中,代表的是不同的对象引用。

 2、这里的prop的类型是*。这是很重要的,因为dict对象中的键可能是任何数据类型的。

以下对Dictionary与Object的不同之处的一个小结:

1、Object的键必须是字符串,或表示字符串的表达式(变量或函数),若不是字符串的表达式,则将调用toString()方法,将其转化为字符串;

2、与Object不同的是,Dictionary使用的键是对象的引用,而不是字符串,且非原始对象键调用之前是通过全等运算符(===)来进行判断,并不会将数据类型强制转换之后再进行判断。

转载于:https://www.cnblogs.com/ddw1997/archive/2011/05/24/2055483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值