Dot Syntax from String

function dotSyntax(target:*, path:String):* {
	var level:Array=path.split(".");
	var curr:* = target;
	for (var i:int = 0; i<level.length; i++) {
		curr=curr[level[i]];
	}
	return curr;
}

trace(dotSyntax(this, "stage.stageWidth"));
trace(dotSyntax(this, "graphics"));
trace(dotSyntax(this, "root.loaderInfo.bytesTotal"));

/*outputs something like:
800
[object Graphics]
1230
*/

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.

Leave a Reply

Your email address will not be published. Required fields are marked *