[jQuery] Namespaces & organization?
> I'm a really big fan of short code.
Me too!
> I'm not saying that there isn't a need for namespacing -
> there definitely is and will be. I just think that we
> constantly need to keep improving jQuery, keeping in
> mind that short function names should be used at any cost.
> That's all for now - let me know what you think on this.
> - John
Instead of adding lots of names in the core stuff, I'd propose using a
method argument for most cases. For example, the event handling currently
puts a *lot* of names in scope: ready, load, mouseover, mouseout, etc. You
could do this instead:
$(document).on("ready", function(){alert("ok")});
Perhaps even allow anonymous objects as input like .css() does:
$("div").on({mouseover: Comin, mouseout: Goin});
It creates syntax that is a bit quotier/curlier, but it's unambiguous since
each argument list is in essence its own name space. It might require more
decoding in the methods like .on() in some cases but the lines of code would
actually be fewer if the old methods were removed.
Another namespace saver would be to use more "little languages" in the
method parameters. We already have little languages in JQuery method
parameters such as CSS selectors, XPath and RegExp. As an example, I had an
app where I was constantly setting and changing classes on items, so I
created a .className() method:
$(tab1).className("+active -inactive");
$(info).className("~hilight");
$(div1).className("viewable");
With the standard JQuery methods they would look this way:
$(tab1).addClass("active").removeClass("inactive");
$(info).toggleClass("hilight");
$(div1).set("class", "viewable");
A little language invocation is nearly always shorter; the code for
.className() is more complex but about the same length as the three methods
it replaces. (You're welcome to the code if you want it.) I also like the
consistency that setting a class name uses the same method as
adding/removing/toggling one.
It might be good to do more of the effects with little languages or argument
lists of some kind, so that we don't get dozens of effect names dropped into
the namespace over time. For example, maybe .show() could handle all the
effects that require fadeIn, slideDown, etc. That could prove more of a
challenge for extensions though...