Functional jQuery as an Array
Hi Everyone -
I just finished adding in two cool new features, that I've been
itching to include for a long time.
1) jQuery is now an array. You can now write code like this:
var s = $("img")[3].src;
should you be so inclined to.
2) jQuery is now more functional (like a functional programming
language, that is), in that it is non-destructive. All 'destructive'
operations (like find, filter, add, etc.) that change the selected
elements can include an optional function, for example:
$("p")
.find("a", function(){
this.style.border = "1px solid red";
})
.filter(".foo", function(){
this.style.border = "2px solid black";
});
which is completely equivalent to doing:
$("p")
.find("a").each(function(){
this.style.border = "1px solid red";
}).end()
.filter(".foo").each(function(){
this.style.border = "2px solid black";
}).end();
much less fuss for equivalent results. Just to clarify: .each and .end
both still exist and work as expected, but there's simply now this
additional shortcut for performing this operation.
Both of these are in SVN (rev 130), the source can be found here:
http://jquery.com/src/jquery-svn.js
along with a wacky demo of both new features in action:
http://john.jquery.com/jquery/test/args.html
Enjoy and let me know if there's a weird bug that I forgot about.
--John