Hi,
I've been reading the recent posts and older archives of
comp.lang.javas cript and am surprised by the sentiments expressed about
the prototype.js library for a few reasons:
1) The library has been referred to as "junk" many times which is a
strong opinion against the relatively high popularity of the library. I
know popularity doesn't make something good.
2) People who use it are advised to "get a minimal clue". (Paraphrased.
Search archives "prototype. js minimal".)
3) Those who criticize so strongly must believe they know better but
the posts I saw containing the criticism did not indicate why the
library is so bad.
I have used the prototype.js library a bit with a mixed experience.
I had very little success with the Enumerable iterators. Constant
problems. They make for code that is bulky enough to match the size of
a JavaScript for loop. In Ruby these types of methods are great but I
don't think they work so well in JavaScript the way prototype.js
implements them.
I have enjoyed using the event parts of prototype.js without any
problems or even reading any of the code. It's usually the goal of a
library to make it so the user doesn't have to know the library's
internals. I'll count this as a success for prototype.js. However,
after reading such criticisms I decided to look at the code just in
case I got lucky so far. It turns out that for my application I was
able to take only about 90 lines of prototype.js's 1800 lines. These
lines are below. I've read them now. They seem ok to me. I would like
to know if anyone thinks this is a weak part of the prototype.js
library and why? Particularly comments regarding the browser
incompatibility workarounds as well as overcoming the IE problems of
the "this" keyword and bubbling (discussed on
http://www.quirksmode.org/js/events_advanced.html).
One thing I wish was that prototype.js used a namespace (discussed
http://blog.dreamprojections.com/arc...2/27/450.aspx). This
would mean the code that uses the library would not look quite so cool
since it would not be possible to extend Event, Function, String, etc.
What do you like or dislike about prototype.js? Any reasons why
prototype.js is "junk"?
I think a critical discussion of this library would be very educational
for many people. I know that I am trying to "get a minimal clue".
Thanks,
Peter
// ------------------------------------------------------------
Function.protot ype.bindAsEvent Listener = function(object ) {
var __method = this;
return function(event) {
return __method.call(o bject, event || window.event);
}
};
function $() {
var elements = new Array();
for (var i = 0; i < arguments.lengt h; i++) {
var element = arguments[i];
if (typeof element == 'string')
element = document.getEle mentById(elemen t);
if (arguments.leng th == 1)
return element;
elements.push(e lement);
}
return elements;
};
// -------------------------------------------------------------------
Event.element = function(event) {
return event.target || event.srcElemen t;
};
Event.stop = function(event) {
if (event.preventD efault) {
event.preventDe fault();
event.stopPropa gation();
} else {
event.returnVal ue = false;
event.cancelBub ble = true;
}
};
Event.observers = false;
Event._observeA ndCache = function(elemen t, name, observer, useCapture)
{
if (!this.observer s) this.observers = new Array();
if (element.addEve ntListener) {
this.observers. push([element, name, observer, useCapture]);
element.addEven tListener(name, observer, useCapture);
} else if (element.attach Event) {
this.observers. push([element, name, observer, useCapture]);
element.attachE vent('on' + name, observer);
}
};
Event.unloadCac he = function() {
if (!Event.observe rs) return;
for (var i = 0; i < Event.observers .length; i++) {
Event.stopObser ving.apply(this , Event.observers[i]);
Event.observers[i][0] = null;
}
Event.observers = false;
};
Event.observe = function(elemen t, name, observer, useCapture) {
var element = $(element);
useCapture = useCapture || false;
if (name == 'keypress' &&
(navigator.appV ersion.match(/Konqueror|Safar i|KHTML/)
|| element.attachE vent))
name = 'keydown';
this._observeAn dCache(element, name, observer, useCapture);
};
Event.stopObser ving = function(elemen t, name, observer, useCapture) {
var element = $(element);
useCapture = useCapture || false;
if (name == 'keypress' &&
(navigator.appV ersion.match(/Konqueror|Safar i|KHTML/)
|| element.detachE vent))
name = 'keydown';
if (element.remove EventListener) {
element.removeE ventListener(na me, observer, useCapture);
} else if (element.detach Event) {
element.detachE vent('on' + name, observer);
}
};
/* prevent memory leaks in IE */
Event.observe(w indow, 'unload', Event.unloadCac he, false);