January 09, 2008 Parsing a nested or name spaced object from a string in ECMAScript

Often times when I am making dynamic scripts in Flash or JavaScript, I want to call an object method using a string. It may be a situation where I am stuck using fscommand (due to a customer request of a Flash version <8) or just writing script with script.

The problem that comes with this method is the advent of ActionScript 3 with Flash CS3/Flex 2 and the rising use in namespaces by JavaScript developers (myself included). Both encour a heavy usage of object methods instead of one-off functions. Dynamically calling singleton functions is easy, you can just use subscript notation, e.g. object[’member/method’]. What you can’t do with this method is pass it any dot syntax and expect it to work e.g. object[’subobject.method’]. The subscript notation is only meant to work one level. So the previous example won’t work. In order to fix this issue and to make sending object methods/members in a single string a little easier, I wrote a function that takes a string and a base object (the default is the window object if no object reference is passed) and returns the method/member. (I know at this point that some will simply direct me to ECMAScript’s built in eval() function, but I personally avoid using it at all costs as it can have unintended, and unsecure results. I’ll let Stephen Chapmen tell you because this article is about something else.)

The function is executed somewhat simply by splitting the string at the dots and adding them as subscripts to one another in order. However, there are some other pieces to it that make it a little more friendly to mistakes and general usage.

  1. function parseNameSpace(s , o)
  2. {
  3. //default to window if no object sent
  4. var w = o || window, a;
  5. //reutrn if parsing not necessary or not a method
  6. if ( s.indexOf('.') == -1 )
  7. {
  8. return (typeof w[s] === "undefined") ? false : w[s] ;
  9. }
  10. //make array for looping through
  11. a = s.split('.');
  12. //run though members / methods
  13. for (var i = 0,l = a.length; i < l; i++)
  14. {
  15. w = w[a[i]];
  16. }
  17. //return object if it is valid, or return false
  18. return (typeof w === "undefined") ? false : w ;
  19. }

The function takes two variables, first the string that you want to parse to an object member, and the second optional base object. The first variable w looks for the object “o” and if not found, defaults to “window” which is the default parent object of browser based JavaScript. (You could change it to “this” and it would work in ActionScript as well). Second, we check to see if the passed string has any dots in it. If not, we check to see if it is a regular function. This way you could use this function as a catch all even if you aren’t always passing nested functions. If it isnt a member of the window object, false is returned. If it does have dots, we split them into an array, and loop through it, making each a member of the previous using the subscript notation. Finally we check to see if the result is valid, and we return false if it isn’t, or a reference to the object itself.

Let’s take a look at a simple way to use it. Lets say we have an object literal with a subobject, and a method inside that.

  1. var obj = {
  2. innerObj: {
  3. func: function(){
  4. return "worked";
  5. }
  6. }
  7. }

Then we retrieve it via string with the function.

  1. var objFunc = parseNameSpace("obj.innerObj.func");
  2. alert(objFunc()); //shows "worked"

This should work most places as long as you are mindful of function scope. Enjoy!

AddThis Social Bookmark Button Posted by Greg Ferrell at 09:37 PM. Filed under: JavaScript • (0) CommentsPermalink