Why I still prefer Prototype to jQuery
blog.thinkrelevance.com
blog.thinkrelevance.com
// Prototype
$A(this.columns).each(function(item) {
this.buildHeader(item);
});
If you want to do that, you have explicitly bind `this` to a (differently named) variable in the outer scope: var self = this;
$A(self.columns).each(function(item) {
self.buildHeader(item);
});In fact, I'd argue that in general seeing uses of this outside of object oriented code make me sit up and look very carefully, because its really easy to get spooky "action at a distance" type behaviour.
$A(self.columns).each(function(item) { this.buildHeader(item); }, this);
In general I prefer the technique you described as it is the most frequent use case and simplifies the implementation of the function that executes the callback.
method.call (<object you want to be *this*>, params);Hmm??
e.onmouseover = function(c) {return function() { c.dosomething()}}(this);
Then in the dosomething function, "this" references what you expect...
I don't see why you need a library to construct a simple closure.
I use both: jQuery for HTTP and DOM traversing/manipulation plus DOM-less prototype core for everything else. This combo is awesome, conflict-free and quite compact when gzipped. Bare bone JavaScript is too weak library wise and Prototype makes an excellent "standard library" for it. I couldn't live without Prototype's extensions to enumerable, strings, arrays, hashes, etc.
And it's 100% compatible with Rails thanks to jRails project. I highly recommend it.
Fundamentally I think Prototype/MooTools are horribly designed javascript library because they introduce incompatibilities. However I use them when I need a free component or 2. I would never drop jQuery though so I always use $j instead of $, even when using jquery by itself
Determine the index of the first parameter in the Array …
Ah, it returns the index. But that’s OK … JavaScript supports the notion of “truthiness”,
Except if it returns "0", of course.It's a very C-like way of doing things:
if ($.inArray(blah blah) < 0)
// not found!but i wouldn't call this article level-headed, it has an explicitly stated tilt towards prototype.
$A(this.columns).each(function(item) {
this.buildHeader(item);
}.bind(this));Some nice speed improvements are coming to jQuery 1.3:
http://www.flickr.com/photos/jeresig/sets/72157612498756067/
the last time i used prototype was in 2007, now it's all jquery
jQuery
$.each(collection,function(){ this.bla(); });
The reason index is first param is because wheb you need it it is the only param. The reason for the item param is incase you need to attach a scope object to be this!