How My 10-Year-Old Learned Javascript
Rich Klein
1.3K32

Only a few things, like arrays can be explained well via analogies. (An array is a row of boxes and a tape measure.)

Why do arrays need an analogy, and a graphics library does not? “You can put something in a numbered place in an array and later retrieve it” will be met with the question: “But why would you? Why not just use a variable?”

The row of boxes analogy is much more powerful than just saying that arrays can be iterated over unlike variables. It gives a hint of what arrays are meant to be used for. For example, it is easy to compare a linked list (a box inside a box inside a box…) to the array analogy and see that arrays have better random access.

It is pretty obvious what the upside of a closure is: they can have internal state. In easier terms: “You don’t have to give them a crapload of arguments, some of which you’d have to drag there from the other side of the world.” Same thing with the graphics library: “It gives you functions that let you draw things easily.”

How I would explain closures:

It is convenient to write code inside a function just like it was in the surrounding scope. But what happens when the function gets passed out of that scope?

In a functional language that is obvious: it just acts as if the environment was still there. In languages where variables can change, the function usually gets references to them, so it sees the most recent value and can even change them!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.