Dot Syntax from String

Actionscript:
  1. function dotSyntax(target:*, path:String):* {
  2.     var level:Array=path.split(".");
  3.     var curr:* = target;
  4.     for (var i:int = 0; i<level.length; i++) {
  5.         curr=curr[level[i]];
  6.     }
  7.     return curr;
  8. }
  9.  
  10. trace(dotSyntax(this, "stage.stageWidth"));
  11. trace(dotSyntax(this, "graphics"));
  12. trace(dotSyntax(this, "root.loaderInfo.bytesTotal"));
  13.  
  14. /*outputs something like:
  15. 800
  16. [object Graphics]
  17. 1230
  18. */

This snippet shows how to parse dot syntax from a string. It does this by splitting the string and then using square bracket syntax. This is one of the main techniques that makes yesterdays post possible.

This entry was posted in dynamic, string manipulation, strings and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*