Simulating pointers in JavaScript

(If you already know about pointers and want to see them in JavaScript, feel free to skip the first few paragraphs).

A basic notion in the C and C++ programming languages is that of pointers. A pointer is basically a variable through which you can access (read or modify) another variable. For example, a function A can call function B and pass it a pointer to one of A’s local variables. Function B can then modify that variable through the pointer, even though it can’t access A’s local variable directly.

Pointers are commonly used to implement “call-by-reference”. An example that we’ll be using here is that of a swap function, that accepts two variables and exchanges their values. After calling swap(x, y) we’ll want to find that x now contains the former value of y, and vice versa. In fact, this isn’t possible in C, nor in JavaScript, since these languages always pass arguments by value. Whatever swap() does, it can’t change the values of x and y, since it only receives a copy of their values. We can, however, give it pointers to x and y, through which it will be able to change their values.

This works great in C/C++, but what can we do in JavaScript, where we don’t have pointers?

One possible way to deal with this is to note that when we call a JavaScript function with an argument x, the function can’t assign a new value to x, but it can modify x’s current value. To make a concrete example:

function f(arg) {
   arg.value = "Hi";
}

var x = {};
f(x);
alert(x.value); // says hi

After calling f(x), x still refers to the same object, but one of the properties on the object has changed. This is a possible way to implement “out parameters”, but it’s not what we want. What if x is an immutable data type (that can’t be modified) such as a Number? Or what if we want to point to an arbitrary property of x?

The solution is using closures. When you define a function inside of another function, the inner function can reference that outer function’s variables. An important fact in JavaScript is that you can pass functions around like normal objects. Closures mean that when you pass the inner function around, the outer function’s variables it references go along with the inner function. Here is a first draft of how closures can be used to simulate pointers:

function swap(ptr1, ptr2) {
    var t = ptr1();
    ptr1(ptr2());
    ptr2(t);
}

function testit() {
    var x = 1, y = 2;

    var xptr = function(val) {
        if (arguments.length > 0) x = val;
        return x;
    }

    var yptr = function(val) {
        if (arguments.length > 0) y = val;
        return y;
    }

    print(x,y); // prints 1 2
    swap(xptr, yptr);
    print(x,y); // prints 2 1
}

testit();

(I’m using the print() function from V8 shell, which isn’t available in web browsers. I’m sure you can find another way to print the results if necessary).

As you can see, the pointer to a variable is actually a function. When you call the function with no arguments, it returns the variable’s current value. When you call the function with an argument, it performs an assignment to the variable.

This is very nice, but writing a 4 line function every time you want to create a pointer can be pretty tedious and error prone. How can you make pointer definitions easier? All this and more in a future post.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: