Oct. 8th, 2006

elfs: (Default)
Unless 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:
 function bind(obj, method) {
    return function() { return method.apply(obj, arguments); }
}
While I've been learning about Javascript, one of the things I learned was a closure. A language with closure basically says that:
  • 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
then, in any call made later to that returned function, the variables scoped to the outer function persist and retain their values at the time the function was returned.

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):
    return function B(x) { return x + y };
}

a = A(7)
a(9) == 16;
a(7) == 14;
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.

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.
elfs: (Default)
Sunday was quieter until about 2:00pm. Breakfast was simple cereal, and I did a lot of housework afterward: scrubbed down the kitchen, did five loads of laundry, that sort of thing, while Omaha took care of most of the financials.

At 2:00, we went to the "Burien Arts Showcase," a series of free concerts and other artsy events being put on by the local arts commission. There were two small symphonys, one heavy on the strings, the other heavy on the winds. The local theater groups did outtakes from two plays they're doing this year, and Kouryou-chan was just entranced. Yamaraashi-chan finally went up with the rest of KidSounds and did her thing; I noticed that they didn't put any of the new kids up there, and that's probably a good thing because they were just not ready. The kids from last year, though, still had their acts together. There was a dance group doing ballet and all sorts of other things. Very, um, folksy. "Community Art," indeed. Yamaraashi-chan is still fidgety on stage but less so than in previous years, but her voice is very lovely now.

Omaha though seems to think that somewhere in her incredibly packed schedule she has time to try out for the local theater group and do something that's purely for her rather than work, family, or politics-related.

After the intermission, Kouryou-chan's mother took her off for a visit down somewhere south of us. I was disappointed; I had hoped they'd watch the rest of the show. Hey, if I had to sit through it, so should they. (Actually, I didn't, as during the parts I wasn't interested in I had my ebook of The Iron Council with me.)

Dinner was the usual Sunday thing of pizza.

Afterward, Kouryou-chan and I folded our clothes, and not only did I figure out the Korean fold technique but it's actually so easy that I taught it to Kouryou-chan, who immediately had to go show Omaha. It's very cool.

Profile

elfs: (Default)
Elf Sternberg

May 2025

S M T W T F S
    123
45678910
111213141516 17
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated May. 26th, 2025 03:25 am
Powered by Dreamwidth Studios