References vs. Pointers in Javascript

I’m as guilty as the next guy when it comes to the confusion surrounding the terminology used for variables that get assigned objects, but I think it’s time to get my act together and define a standard at least for myself:

If “new Object()” is an OBJECT, then “var obj=new Object()” makes obj a POINTER. Why is that?

The important thing here is considering what it is not. It is definitely not the actual OBJECT, because if that variable were the actual OBJECT (even if only at that particular moment), then “var obj2=obj” would have to make obj2 its own OBJECT too. But it doesn’t: Instead it points to the same OBJECT as obj does.

So that means that obj can only be a POINTER or a REFERENCE, since it doesn’t actually contain the OBJECT like it does for strings or numbers.

It’s tempting to call it a REFERENCE and I wouldn’t object to it if it weren’t for the fact that Javascript actually has real references, meaning variables that if set will cause another variable to change its value. This happens every time you access the arguments of a function:

(function(myArg){
    alert(myArg+" "+arguments[0]);
    arguments[0]="SET-BY-REF";
    alert(myArg+" "+arguments[0]);
    myArg="SET-BY-VARIABLE";
    alert(myArg+" "+arguments[0]);
})("INITIAL");

Which will print out “INITIAL INITIAL”, “SET-BY-REF SET-BY-REF”, “SET-BY-VARIABLE SET-BY-VARIABLE” in that order.

To be honest, this isn’t really something that you’ll encounter very often and it rarely is of much use, but it definitely makes myArg a REFERENCE of arguments[0], (meaning that any change to variable 1 will be reflected on variable2 – almost. In this case a change to a property will be reflected on a variable and vice-versa, but it’s so very close that it’s difficult to imagine that you’d use the term for variables pointing to an object, but not for this).

So, if obj can’t rightly be called a REFERENCE or an OBJECT, it has to be something else. And pointer seems to fit the bill very nicely, so that’s what I’m going to call it. Let’s see if it catches on 😉

5 thoughts on “References vs. Pointers in Javascript”

  1. Yeah, generally we speak of object (references) and primitive values.

    Anything in js that’s not a primitive value is automatically an object. A primitive value is one of boolean, number, string, null, and undefined. And yeah, regex literals create a regex object. Just as the object literal and array literal do.

    Although technically correct, hardly anybody uses the arguments “hack”. In fact, you’ll find few people actually know what’s going on or that the magic link even exists. It only exists for the arguments object, by the way.

    Primitives are implicitly promoted to an object if you try to access a property on them. Setting or deleting their properties silently fails.

    So in the js community, we usually speak of “object references” for any object. We know not these pointers you speak of.

  2. I know, it rarely comes up in discussion, so the problem is really minimal. To be honest, this is just the sort of nitpicking I enjoy after work hours… chances are that less than 10% of all JS developers know it and less than 1% have ever encountered it in real-world code. Still, it’s a nice tool to make people think about the handling of variables in Javascript and making people think is always a good thing 🙂

  3. Need to pull something from active directory using the person’s ID number in the company database. Purpose of writing the script is to delete everyone’s name that is in a specific drive in the company’s intranet. Can you help?

  4. In Javascript you have no control over “value” vs. “pointer”. For Objects and Arrays, what’s stored is always a pointer (and when passed as function arguments they are “byref” so what the function does is also visible to the caller). For simple types, what’s stored is always the value (and when passed as function arguments they are “byval” so what the function does cannot affect what the caller sees). Simple types _may_ or_may_not_ be encapsulated as an Object, and so get different treatment; this is the difference for example between “String” (wrapped as an Object) and “string” just the naked value.

    What you’re calling a “Pointer” is actually called an “alias”.

  5. I can live with alias, just as with pointer … The problem I had was that as soon as you start working with the arguments object, you’ll get the confusing situation that you’re suddenly dealing with actual references… You can’t control them, but they do exist in Javascript. So, suddenly you have two very different things that people call by the same name.

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.