I’ll be using a programming jargon in this post, so excuse me if you don’t understand things immediately. All explanations are at the bottom.
One thing I love about PHP and sorely miss in C++ is the concept of variable variables. A ‘variable’ variable is when the identifier of a variable is a variable itself. Any language which supports this, will also support other variable controls. If you know what I’m talking about, skip the next section.
‘Variable’ variable
I love the feature. It saves a lot of headaches if you’re not sure of the incoming requests, and you need to serve all kinds of situations. I used it when when I was programming an API for a project I was working on.
It works by taking a string as the name, and then defining a variable with that name. That variable can then be used as a normal variable. Unfortunately, the way this works, you’ll need to have one variable which will contain the identifier as a string. If a language natively supports it, it’s much easier to work with it. However, when there are cases where you do need to use this in Javascript, there is a rather simple way to do it.
‘eval’ is your friend
Using the eval function creatively allows us to create these variable variables and function which can be used throughout. Of course, it’s simpler if you define a function to handle your variable, and do what you would expect a manually defined variable of that sort to do. Take a look at this:
function get(v){
flag = 0;
if(typeof(eval(v)) == 'function'){
for(i in eval(v+'.prototype'))
flag = (i) ? 1 : 0;
if(flag == 1)
return eval('new '+v+'()');
else
eval(v+'()');
}
else
return eval(v);
}
Now, whenever you need to work with your variable, just call the function, passing the name as the argument, and it’ll return whatever you expect. The extra bit in the condition for == 'function' is because Javascript treats classes as functions. So we need to check if its prototype has any methods defined. If it does, it is a class, and we return an object of that class, else we just call the function. Hopefully this takes all cases into consideration. If you have a faster way, please let me know! Ruski points to the window[x] method as a faster alternative in the comments.
A practical use of this would be if you wanted to write a script to receive a request for a function, which belongs to a class. I’ll take the example of the Facebook method users.getInfo which returns the info on a user based on the UID and an array of fields passed. Now, I’ll skip the actual definitions, but assume something like the following:
users = function(){
/* ... characteristics ... */
};
users.prototype = {
/* ... methods ... */
getInfo: function(){
/* ... function body ... */
}
}
So now, if the page is passed users.getInfo, we can do the following:
request = 'users.getInfo';
method = request.split('.');
class = get(method[0]);
get('class.'+method[1]);
That’ll execute the required function. The beauty of this is that you can change the string, and the code remains the same, with result being that the correct method of the required class will get executed.
Of course, if you’re programming at that level, you’ll be better off working with a more powerful server-side language like PHP. But this is a nice way to use those concepts in Javascript if you ever need to. If you have improvements to this, please feel free to shoot them my way :)
2 Comments
The eval() function works for this, but is very inefficient. You’d be much better off evaluating such variables through the use of brackets. For example, since all global variables are put into the window class by default, then if you have something like: var y = ‘some string’ var x = y
You could get to the value of y through x by window[x], which would be much more efficient in terms of resources than using eval().
I found out about the
window[<name>]method after I wrote this (it’s been long). Yes, that method is definitely faster, and for all practical purposes, serves our need.Leave a comment
RSS feed for comments on this post. TrackBack URL