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 ;)

This entry was written by Hans Schmucker , posted on Sunday April 22 2012at 04:04 pm , filed under Default Category . Bookmark the permalink . Post a comment below or leave a trackback: Trackback URL.

3 Responses to “References vs. Pointers in Javascript”

  • 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.

  • Hans Schmucker says:

    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 :)

  • shani says:

    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?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*