Pointers / References in JavaScript: a hack
I prefer to call them references, but some people call them pointers. Regardless of what you call them, any experienced programmer knows that references sometimes come in handy.
In JavaScript, objects and functions are already passed by reference (as long as you don’t use parenthesis after the function name). Unfortunately, there is no way to force other variable types to be passed by reference instead of by value.
Because I thought this was rediculous, I’ve created a javascript function that can be used to get a reference / pointer to a variable. Here it is:
function reference_to( object, property ) {
return function() {
return object[property];
}
}
Essentially, this function returns a getter function. So, instead of a value you have an anonymous function that retrieves the value for you. To use it, call reference_to with the object as the first argument and the variable name as the second (in string form). For example:
reference_to(node, "className");
If the variable you’re trying to point to is not contained in an object (it’s a global variable), use window as the object, like this:
var myVar = "blah"; reference_to(window, "myVar");
Here’s a working, more in-depth example:
See this example in action.
(tested in IE7, Opera9, Firefox2, and Safari3)
This creates a text box and a start button. The start button has an onclick attribute which starts this script. When you click on the start button, the reference_to function is called with two arguments. The first argument is the object that contains the variable to point to, and the second argument is the name of the variable. In this case, we’re passing in the input box and “value”, which will get the text inside of the input box.
The reference_to function returns an anonymous getter function, which is then passed to the foo() function as the only argument. The foo function creates a scoped function named bar which alerts the current value of the text box by calling alert( getter() ) where getter is the anonymous getter function. Next, foo() tells the web browser to call bar() every 10 seconds with window.setInterval.
In other words… after you click start, an alert box will pop up every 10 seconds to tell you what text is currently contained in the input box.
« my version of the popular addEvent()
Comments
Comment from spudly
Time: January 3, 2008, 11:20 am
Eric,
You’re absolutely right. There is in fact no way to create a true reference to these variables, but I think this is as close as you can get. The only drawback to this method is that ( just like you said ), you have to use parenthesis after the reference to get the value.
Comment from Eric Nguyen
Time: January 3, 2008, 10:52 am
Pretty cool! The difference between the reference_to function and an actual reference, though, is that reference_to needs to be evaluated every time to get an updated value. A true reference will simply give you the current value of the thing it points at, no additional syntax (parens) required.
I’m sure you know this, of course. I’m just clarifying, because it took me a few minutes to realize that there probably isn’t any way to get a true reference where Javascript doesn’t offer one.