Geek Enlightenment
Oct. 8th, 2006 09:15 amUnless you're a geek, this is going to be meaningless. For months now I've been playing with prototype.js, a Javascript package that does amazing things with packaging and associating code. Many people think that prototype is a kind of miracle. My reaction when first using it was that it must be doing something odd and unusual, exploiting some unintended consequence of the ECMA Javascript standard. I tried to wrap my head around its core concepts and failed. I knew the core concept was in a method called bind, that looks like this:
I now understand why progamming is so hard to learn past the basic concepts: instructors are allowed to write the crap like the above when, really, the only way to grok it is to write the code several times until you're comfortable using it. Then you need the vocabulary to explain why it works! In any event, most of the languages I'd worked with up until now didn't have closures; OO was a conceptual addition to languages like Perl and C, and in both languages the addition is simply ugly.
Anyway, an example (in javascript):
But prototype.js isn't just "object oriented lite". It's a full-blown OO layer on top of Javascript. And the secret is in bind.
Once you understand closures, go back and look at bind up above. The enlightenment came to me this morning. It was a painful, "Duh, oh frack, that's how it works, man am I stupid" kind of enlightenment. bind just says, "There's this thing, and this function associated with the thing, and whenever you call the function you want that particular thing, the one given to you just now, to be a thing you know about." Then I had a grin because I had the most clear realization: Prototype.js isn't doing anything with the language that the language wasn't intended to do. It is, in fact, the sort of thing the ECMA standards writers thought was possible and expected people to do with the language in the first place.
The more I learn about the ECMA standard for Javascript, the more I like the language. It's a full-blown scripting language, and it's typing has the same hijinks as Perl or Python, but the inner syntax has a purity and elegance that makes it much more admirable than the language from which marketers stole its name, hoping to give it cachet.
I don't mention my current language of choice, Python, in the list of languages I'd used that didn't have closures: because it does! I tried it out and was even more delighted to learn that Python has first class closures just like Javascript.
function bind(obj, method) {While I've been learning about Javascript, one of the things I learned was a closure. A language with closure basically says that:
return function() { return method.apply(obj, arguments); }
}
- if you declare a function
- and declare another function within that function
- and the inner function references variables scoped in the outer function
- and the outer function returns a reference to the inner function
I now understand why progamming is so hard to learn past the basic concepts: instructors are allowed to write the crap like the above when, really, the only way to grok it is to write the code several times until you're comfortable using it. Then you need the vocabulary to explain why it works! In any event, most of the languages I'd worked with up until now didn't have closures; OO was a conceptual addition to languages like Perl and C, and in both languages the addition is simply ugly.
Anyway, an example (in javascript):
function A(y):etc.. You see how A returns a function (now labeled a), and the value passed into that function is retained by the B function as it's used? Kinda cool: a cheap way to get object-oriented code.
return function B(x) { return x + y };
}
a = A(7)
a(9) == 16;
a(7) == 14;
But prototype.js isn't just "object oriented lite". It's a full-blown OO layer on top of Javascript. And the secret is in bind.
Once you understand closures, go back and look at bind up above. The enlightenment came to me this morning. It was a painful, "Duh, oh frack, that's how it works, man am I stupid" kind of enlightenment. bind just says, "There's this thing, and this function associated with the thing, and whenever you call the function you want that particular thing, the one given to you just now, to be a thing you know about." Then I had a grin because I had the most clear realization: Prototype.js isn't doing anything with the language that the language wasn't intended to do. It is, in fact, the sort of thing the ECMA standards writers thought was possible and expected people to do with the language in the first place.
The more I learn about the ECMA standard for Javascript, the more I like the language. It's a full-blown scripting language, and it's typing has the same hijinks as Perl or Python, but the inner syntax has a purity and elegance that makes it much more admirable than the language from which marketers stole its name, hoping to give it cachet.
I don't mention my current language of choice, Python, in the list of languages I'd used that didn't have closures: because it does! I tried it out and was even more delighted to learn that Python has first class closures just like Javascript.