as3 读书笔记

仅仅是笔记 没有讨论问题 大家请略过

1.用正则来replace字符串

  1. var str:String = "above thE cloud,around The shadow.";
  2. var pattern:RegExp = /the/ig;
  3. trace(str.replace(pattern, "my"));
  4. //above my cloud,around my shadow.

2.数组深度copy

  1. private function clone(source:Object):*
  2. { 
  3.     var myBA:ByteArray = new ByteArray();
  4.     myBA.writeObject(source);
  5.     myBA.position = 0;
  6.     return(myBA.readObject());
  7. }

3.遍历Object,这个和as3没什么关系,不过经常用到。

  1. private function traceObject(obj:Object, indent:uint = 0):void
  2. {
  3.     var indentString:String = "";
  4.     var i:uint;
  5.     var prop:String;
  6.     var val:*;
  7.     for (i = 0; i < indent; i++)
  8.     {
  9.         indentString += "\t";
  10.     }
  11.     for (prop in obj)
  12.     {
  13.         val = obj[prop];
  14.         if (typeof(val) == "object") 
  15.         {
  16.             trace(indentString + " " + prop + ": [Object]");
  17.             traceObject(val, indent + 1);
  18.         }
  19.         else
  20.         {
  21.             trace(indentString + " " + prop + ": " + val);
  22.         }
  23.     }
  24. }

Leave a Reply