The conversation ranged pretty far and wide. Be warned that the last 15 minutes or so are a bit of a venting session about where some of the students are feeling frustrated with the Udacity’s Front-End Web Development Nanodegree curriculum.
One of those things was Haverbeke’s metaphor for variables and values. He compares variable names to tentacles and values to things that those tentacles can grasp or grab onto. That is in contrast to another common metaphor in which variables are imaged as boxes and values as things that can be put in those boxes. The relevant text is on page 23:
You should imagine bindings as tentacles, rather than boxes. They do not
contain values; they grasp them—two bindings can refer to the same value.
A program can access only the values that it still has a reference to. When
you need to remember something, you grow a tentacle to hold on to it or you
reattach one of your existing tentacles to it.
A strength of this metaphor is that it makes it clear that (at least in some cases) more than one tentacle can grasp the same value. Objects are passed by reference in JS, which means that if you assign two different variables to an object value, both variables point to the same object in computer memory. The place where this metaphor doesn’t quite work is that primitive values are passed by value, not by reference. Two tentacle cannot grasp the same string or the same number. Instead, if you declare two variables and assign them to the number 5, each variable will point to its own 5, that is, to two different spots in memory. Once a tentacle grabs a primitive value, not other tentacles can grab it.
Another place where the octopus and tentacles metaphor helps makes sense of what’s going on in JS is that tentacles never disappear but the things they grab onto do. If there’s nothing that’s “grabbing onto” the value, then it will be “carried away by the current,” so to speak. The value will be garbage collected, because values only stick around while there’s something that points to them or references them.
Honestly, that’s all that really stuck out for me from Chapter 2, but I’d like to hear below from other folks, if they had other thoughts about the reading.
Our next meeting is next Thursday, the 26th at 9:00 PM ET. We’ll be reading chapters 3 and 4. Please RSVP here: