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.
jjinux said…
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...
jjinux said…
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.
jjinux said…
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!

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from