MacPython Logo from __future__ import *

Kailash and Friends Kailash Kher Kaipa

online mp3 Anoice albums buy Amund Maarud albums online Asia online CD Andy M. Stewart buy tracks Axis online Astral Rising A Beautiful Machine download CD Aereda buy tracks Aksent online tracks Absidia Atrium Carceri A Beautiful Machine Absolum buy CD Aryan Wind and Brumalis and Valhalla Saints online music Atomsmasher download albums AK1200 download music Angelzoom online CD Arturo Mantovani and his Orchestra buy music 16 buy tracks Ashtorath online CD Aimee Mann buy music Anael And Bradfield buy mp3 Autumnblaze download mp3 Aggrolites download CD Arj Snoek buy albums Ada buy CD Aalto Andy With Rama West A Beautiful Machine Absolum online tracks Asura albums online Albert Lee 4 Non Blondes A Beautiful Machine Absolum download albums Andrew Lloyd Webber and Ar Rahman online music African Head Charge download mp3 Amber Asylum online music Analena online music ANTIX feat ROB SALMON A.R. Rahman A Beautiful Machine Absolum online tracks African Blackwood buy mp3 Axis buy mp3 Alan Menken buy music Amoebic Dysentery buy Alph Secakuku A Beautiful Machine albums download Albita online Amparo Ochoa A Beautiful Machine download tracks Andy Partridge and Harold Budd download tracks Anubian Lights Alient Project A Beautiful Machine Absolum buy albums Antonio Forcione download CD Ali G Indahouse online mp3 Art and Jazz Messengers Blakey download Arab Strap A Beautiful Machine online albums Adema buy Agua de Annique A Beautiful Machine buy CD Avalanches download tracks Acroma Andi Deris A Beautiful Machine Absolum download tracks American Steel download albums Amanda Perez online 999 A Beautiful Machine download mp3 Arild Andersen download CD American Steel buy tracks Absolute Beginner download tracks Anubi online albums Ancient Wisdom online A Verse Unsung A Beautiful Machine buy music Aghast Andromeda Island A Beautiful Machine Absolum download Arlo Guthrie A Beautiful Machine online mp3 Aavepyora online albums Achillea buy Andrew Bird A Beautiful Machine buy music Alexey Aigui and Ensemble 4'33'' albums buy Abbey Lincoln and Archie Shepp download albums Archive download CD A Guy Called Gerald feat. D.S. download music Al Di Meola online music Abigail download music Angel Witch online music Adelaide

2005-07-01

JavaScript frameworks

Filed under: AJAX, MochiKit, javascript — bob @ 3:12 pm

JavaScript, as a language, gives you just about nothing useful. All you have are objects (little more than key-value bags, or null), extremely lame exception handling, and a few primitives (number, string, undefined, function). That means no classes, macros, continuations, coroutines, metaclasses, etc. Forget even thinking about overloading operators or inventing new control structures.

To make JavaScript a half-assed environment for writing code that does more than twiddle the visibility of DOM elements, you have to really add a lot of junk to it. There are two or three out there worth their salt, but all of them have their issues. Here's my beef with Prototype, for example:

  • Object.prototype hacking breaks for (key in obj) syntax in just about every imaginable way
  • No documentation
  • No tests
  • No good method for doing deferred operations
  • Crufty code evolved from JavaScript frameworks past that attempts to work around bugs in browsers that wouldn't even consider Prototype valid code in the first place!

There really isn't enough useful code in Prototype to make it worth my while to embrace and extend it, so I went and wrote my own (MochiKit). It's not released yet, but we're planning to put it out there this month. Currently it has some really interesting stuff, though. Here's just a small taste.

MochiKit/Base.js:

  • A complete set of operator-wrappers for use with functional programming (e.g. operator.add, operator.truth, etc.)
  • Wrapper functions for common operations (itemgetter(nameOfKey), forward(forwardMethodName), typeMatcher(tA, tB, ...), isUndefinedOrNull, etc.
  • Partial function application partial(func, arg0, arg1, ...) and function-binding bind(func, obj).
  • Functional programming constructs for Array-like-objects and argument lists: map(fn, seq0, seq1, ...), xmap(fn, p0, p1, p2, ...), filter(pred, seq), xfilter(pred, p0, p1, p2, ...), concat(seq0, seq1, ...)
  • An adapter registry concept that makes certain bits of the framework more like generic functions.
  • COMPARISON THAT WORKS. compare(a, b) for any reasonable a and b will do the right thing. This includes arrays, dates, numbers, and strings. If you want to teach it how to compare something else, there is registerComparator(...) which is a wrapper around an adapter registry! Also, keyComparator(...) and reverseKeyComparator(...) that conveniently wrap up compare(a.key, b.key).
  • repr(obj) gives you Python-like repr for objects, if obj.repr() is implemented OR if there is an adapter for it via registerRepr(...). Built-in repr for array, string, undefined, and null. The default is obj.toString(), of course.
  • min/max functions that use compare(...): listMax(lst), listMin(lst), objMax(p0, p1, ...), objMin(p0, p1, ...)
  • nodeWalk(node, visitor), a non-recursive object walker (great for finding things in DOM trees).

Every feature has tests. Tests are run on Safari, Firefox, and IE. The tests all pass.

Every function has documentation. The documentation is in reStructuredText. A documentation tool extractor and generator will ship with MochiKit.

I'll save talking about Async, DOM, Iter, and DateTime for a later entry. Needless to say, they're much cooler than Base.

UPDATE:
You can check out MochiKit at mochikit.com! We ended up splitting the docs and source, though, so there is no doc extraction tool. It's still reStructuredText, though!

13 Comments »

  1. You’re such a tease!

    Comment by Andy Gross — 2005-07-01 @ 5:43 pm

  2. This sounds very interesting, but what about opera. How does the tests run there?

    Comment by Even — 2005-07-01 @ 8:23 pm

  3. I don’t care about Opera. If someone else does, it will be open source, and patches will be accepted.

    Comment by Bob Ippolito — 2005-07-01 @ 8:38 pm

  4. Sounds like you’re implementing Python-in-Javascript, or at least Python protocols in JavaScript. Too bad you’re not keeping the special names the same, too, or we could maybe write a Python->JS translator. (I know, I know, too many limitations in JS, so it’d be a Python subset at best.)

    Comment by Phillip J. Eby — 2005-07-01 @ 8:52 pm

  5. Special names are only useful if you want operators or something. JavaScript doesn’t let you do anything about those.

    Anyway, I don’t see how it could stop you going from Python -> JS, you simply remove the __uglies__. I am keeping the names, just not the __turds__.

    Comment by Bob Ippolito — 2005-07-01 @ 8:58 pm

  6. Would love to check it out when you release. Having to live with JS ugliness on a daily basis with FeedLounge, I would welcome a library with some sanity behind it, rather than the usual “doesn’t work in this browser, so we hack it” tricks.

    Comment by Scott Sanders — 2005-07-02 @ 10:45 am

  7. You should be sure to announce to Web-SIG when you release this, as there’s lots of people there interested in a library.

    Personally I like some of the stuff in Prototype. I like $, and I like Object.extend (except I’d be fine if it was just an extend function). There’s clearly some pieces missing as well, and some things I don’t understand.

    Incidentally, Mozilla is adding Array methods for map and other operations. I would recommend implementing those for other browsers, rather than implementing function-based alternatives.

    My personal dream library would be a combination of What We Already Have, and What Javascript Should Have Had All Along — all documented, and presented as a single whole. Kind of like a set of Javascript that is vetted for reasonableness.

    I hope you also pay attention to the memory leak problems in IE; mostly creating the necessary interfaces to be able to break references on page unload.

    Comment by Ian Bicking — 2005-07-03 @ 3:09 pm

  8. MochiKit has getElement, which is the same as Prototype’s $ — but we support the $ syntax for convenience/compatibility.

    MochiKit has an extend function, but it is like list.extend, not dict.update. I don’t currently have anything in there like dict.update, but it would be easy to add (though it would be a function, not a prototype hack).

    Mozilla adding array operations is nice and all, but MochiKit has them as functions for two reasons:
    1. No built-in prototype hacking
    2. It works for array-like objects (DOM NodeList, etc.) as well as arrays. Also, MochiKit.Iter’s imap, etc. work with anything that is iterable (and you can write adapters to make anything iterable).. so, shove that in your Mozilla pipe and smoke it ;)

    AFAIK the memory leak issues in IE are with event handlers. Currently MochiKit only sets event handlers for the body and XMLHttpRequest, and both get unset as soon as it’s done. Cycle breaking for IE is certainly on our radar, but it’s not going into the library until the library has widgets in it.

    Comment by Bob Ippolito — 2005-07-03 @ 3:16 pm

  9. I want to hug you! Finally real programmers start using JS, and making it useful for others!

    Comment by Tuure Laurinolli — 2005-07-07 @ 5:20 pm

  10. Just a sidenote: memory leakage in JS occurs both with events and with cross-references between DOM nodes and JS objects. The following will already cause a leak:

    function F() {};
    F.prototype.leak = function() {
    var node = document.createElement(’div’);
    this.domnode = node;
    node.refobj = this;
    };

    var f = new F();
    f.leak();

    Every time this function is called IE will cause a systems memory to fill up a bit (and won’t release it when the page is unloaded)…

    Comment by Guido Wesdorp — 2005-07-29 @ 2:53 am

  11. Your Prototype-bashing is not a good thing. First of, the latest Protoype beta does away with manipulation of Object.prototype. Second, “..wouldn’t even consider Prototype valid code..” -> Prototype is perfectly valid JavaScript 1.5. Also, Prototype includes support for non-memory leaking events. Documentation is still is lacking, true, but Prototype has sprung into life as a JS framework for Ruby on Rails (and the helpers are documented), and has grown beyond that original goal. Note the new wiki at http://wiki.script.aculo.us for some documentation.

    I wonder why we can’t get along without the bashing. Anyway, good work on MochiKit.

    Comment by Thomas Fuchs — 2005-08-07 @ 7:28 am

  12. Look at the date on the entry. At the time, all those things were true about Prototype. If it weren’t for being corrected, I doubt they’d have tried to fix it.

    If they had written a library worth using, I wouldn’t have written MochiKit, because the Prototype license is right. It’s just that the rest sucked, and it’s not getting better all that quickly. The entirety of MochiKit was developed, documented, and tested in the past 6 or 7 weeks - and what have they done? Released a hard-to-find beta that fixes a couple things they should’ve done right at the start? Whatever. It’s not really on my radar anymore.

    Comment by Bob Ippolito — 2005-08-07 @ 5:42 pm

  13. Fresh Logic Studios Scripts is an object oriented JavaScript framework with a programming model similar to the Microsoft .NET Framework.

    We created this to help save us some time in developing our own products and didn’t feel it was fair to keep this work all to ourselves. Inside you’ll find JavaScript implementations for a small subset of the classes provided by the .NET Framework base class libraries.

    Fresh Logic Studios Scripts is lightweight, weighing in just over 10KB. Download the library, view examples and documentation @ http://scripts.freshlogicstudios.com/

    Comment by Shawn Miller — 2006-10-03 @ 6:31 pm

RSS feed for comments on this post.

Leave a comment

Protected by WP-Hashcash.

Powered by WordPress