Code Ramblings

Code. Rant. Repeat.

Beware of the JavaScript reference!

As you (hopefully) know, variables in JavaScript are actually references. As you (most surely) know, references can bite you in the ass in the most unexpected places, so here’s one of them. Say you have an object that you continuously change (e.g. recursively) and want to see how it “evolves” by calling console.log() on it. You would expect to see “snapshots” of the object logged, right? Wrong. For example, try running this code in a console (Note: I have only tried this in Chrome, not sure how other browsers behave):

1
2
3
var mutant = {"name": "Leela Turanga"};
console.log(mutant);
mutant["characteristic"] = "One eye";

Now, expand the Object and voilà, you’ll see both properties. This can be very confusing with more complicated objects. If you’re too lazy to run that code, here’s proof:

As a solution, you can clone the object before logging it. Here are a few suggestions for how to do that.

Comments