Skip to main content

JavaScript: Quickies

Concerning MochiKit: It's well written. I like the Pythonic feel. I like the docs, the humor, and the heavy regression testing. I even like the code, which is a huge compliment!

Concerning DojoToolkit: I like the fact that it's a community of people who have each given up their own framework to work together. I like the mailing list. I like the event system and the package system (although I haven't used it yet). I don't like the fact that it doesn't feel quite as polished by a perfectionist as MochiKit. I also hate the part of the style guide that says you should not use a space before "{" in:
if (foo){
}
I don't know of any other style guide that suggests this.

Does this mean I'm going to pick one over the other? Nope. Both have good parts. Fortunately, they're compatible. It looks like I'm going to have to use them both. Ideally, the guys from MochiKit would take a bunch of their stuff and shove it into DojoToolkit. Then the guys from DojoToolkit would change their style guide. (Yes, I readily admit I'm a freak. No, my psychologist doesn't think there's anything he can do for me. ;)

You may wonder why I didn't mention Prototype. I've heard that it has no documentation and that it doesn't play nicely with other JavaScript libraries. I'm sure that it's very nice and that it has a lot of cool features, but these are things that I feel are important.

Also, I just read "DHTML Utopia: Modern Web Design Using JavaScript & DOM". I'm really pleased to find a modern JavaScript book. It was short, yet I got a lot out of it. On the other hand, there were many errors. I think the editing left something to be desired. Furthermore, it reinforced my belief that the intersection of good JavaScript programmers and really good software engineers is really small. There were many code snippets where too much code was duplicated instead of put into some utility function. I wish they had a technical editor who suffered from chronic analness like I do. Nonetheless, it was a pretty good book, and I thank the author for writing it.

As a last note, why have I made it through two DHTML books yet never had the prototype system explained as well as this paper explains it?

Comments

Thanks for the compliments on DHTML Utopia. I'm sorry to hear that you found errors, although I fear that wrapping everything up in utility functions makes for tight code but complex and fragmentary explanations. Nonetheless, the words of wisdom are appreciated.
Hey Stuart, thanks for your response! I definitely understand where you're coming from, and that had indeed occurred to me. Just as you made a function for addEvent, I wish you would have made a function for:

var target = window.event ? window.event.srcElement : e ? e.target : null;
if (!target) return;

Similarly:

if (window.event) {
window.event.cancelBubble = true;
window.event.returnValue = false;
return;
}
if (e) {
e.stopPropagation();
e.preventDefault();
}

(Hmm, Blogger won't let me indent things or use a pre tag.)

That would have saved a lot of duplication.

Anyway, thanks again for an extremely useful book! I definitely feel like I have a masterful grip on the subject now.
I understand the theory behind wrapping those things, but it's not easily possible. For example, I can't just create a "getTarget" function, because if no target is available then I have to return from the event handler. So, my handler would have to say something like:

var target = getTarget(e);
if (!target) return;

which is not a lot simpler than what I actually *do*, and requires me to set up another function...
Yep, I understand that completely. That's why it's nice to have a wrapper (perhaps put in place by addEvent) so that by the time your event handler is called, the event is passed to it and that event object is sane and has a sane method to cancel propogation, etc. That is, all these difficulties can be straightened out before the application's event handler even gets called. That's what Dojo does, of course, and I don't think it'd be too hard to have addEvent setup this wrapper implicitly. Of course, I definitely respect your argument that you were afraid it would decrease readability.
I agree with the idea that that's what Dojo does, and there's a reasonable case for using Dojo or some similar library in your code. However, I thought it was important to show people what's actually going on under the covers; using Dojo might make you more efficient, but it's a job for "Dojo Utopia". I admit that I used the Sarissa library instead of raw XMLHTTP, but XMLHTTP stuff falls the other side of the "this is too much of a pain to do manually" line, to my mind.
Sorry, perhaps I wasn't clear. I wasn't suggesting that you teach Dojo. Afterall, I'm interested in how to write libraries like Dojo from scratch. I'm just saying that you made addEvent into a useful library function. It wouldn't be much harder for addEvent to automatically generate a wrapper function for each event handler so that that event handler got an event object that had a reasonable, cross-browser API. It's something that you could discuss in the book when you discussed addEvent. Then, for the rest of the book, you could just use it without thinking about it. Hopefully that was more clear. If you'd like, I can send you the kind of code that I had in mind. Thanks again for your responses!