In my recent post on creating a jQueryUI widget, I referenced the concept of self-executing anonymous functions. I've actually had a few questions come up at the office lately about how they work, so I figured turning it into a blog post might not be a bad idea. It's an important concept in Javascript many don't know about. Others know how to use them, but don't really understand how or why they work. Today I'll cover step-by-step how to go from a traditional function to a self-executing anonymous function; hopefully it will be clear at the end how these things work.
» Read more: How Self-Executing Anonymous Functions Work
Posts Tagged ‘jquery’
How Self-Executing Anonymous Functions Work
September 24th, 2009Using Sizzle with Prototype
March 24th, 2009Recently, John Resig of jQuery fame released the selector engine used in the new version of jQuery called Sizzle. Sizzle is a new take on using CSS selectors within Javascript and aims to be far more efficient than the methods commonly used by most current Javascript libraries.
Currently, I'm working on a large project that is nearly ready to deploy. Most of the Javascript for this project has been written using the Prototype framework; however, we wanted to leverage the speed of the new Sizzle selectors engine in our project. Given that we are deep in the QA process of the project, large code changes are discouraged to ensure code stability. What we needed was a quick way to integrate Sizzle with Prototype.
The following code can be added to your Prototype 1.6.x file and will ensure that prototype's main element collection methods (Element#select and $$) both use Sizzle. I've written before on how slow $$ can be, so these are two quick wins for Prototype performance.
//Overwrite findChildElements to use Sizzle http://sizzlejs.com
Selector.findChildElements = function(element, expression){
expression = expression.join(", ");
var results = Sizzle(expression, element);
if(results.length > 0){
for(var i=0; i < results.length; i++){
results[i] = Element.extend(results[i]);
}
}
return results;
};
Which JS Framework is “The Best”
June 27th, 2008After my recent tutorial was posted on NETTUTS, I found the feedback filled with comments such as:
Jquery is the best and can be used to do all of this stuff plus more.
and
Yeah, I agree NETTUTS should only go away from jQuery when it can’t perform something
Reader Tim commented:
Nice. but which is best? Scriptaculous, Mootools, jQuery, or Dreamweaver CS3 built in apps?
What amazes me is not the sentiment so much as it's the nearly religious zeal with which these people are dedicating themselves to a code library. So I wanted to take a moment to examine the question without that same type of bias--Which JS Framework really IS "the best?"
To attack this problem, we should first really define what "the best" means. After all, if some code library is worth this type of devotion, I want to be pretty damn sure that it's the best! So what does it mean to be the best Javascript library? Here are some thoughts:
- It's the smallest
- It's the fastest
- It provides the most functions
- It provides the strongest application structure
- It provides a plugin system
- It provides clean methods to implement Object-Oriented Programming
- It integrates with a powerful back-end development platform
- It provides easily maintainable and extensible code.
- It's the most widely adopted by the biggest sites
That list was literally written in 30 seconds off the top of my head; I'm sure the true criteria to establish which Javascript library is the best is a far longer list. That said, we'll use these 9 criteria. What do we notice about these requirements? If you answered that some of them lie in nearly direct opposition to each other (such as 1 and 3) you're right. If you answered that some of them can compensate for weaknesses in other areas (for example, 5 or 8 and 3) you're also right. Even with a short list of 9 requirements, we know two things:
- No one library is going to meet all of these criteria
- Not all of the requirements are always requirements
If you're busy debating which Javascript framework is "best," you're fighting a religious war that cannot be won; simply put--no one Javascript library can be objectively defined as "the best."
Gee, thanks Brian. But which should I use?
In my opinion, for every project, you should evaluate which library makes the most sense to use. It is certainly reasonable to have a preference (for example, you may feel that jQuery works for about 75% of all the project work you do) and that's great. You may even find areas of overlap where you continue to use your preferred framework simply because it makes sense. For example, if you know prototype very well but are less experienced with jQuery, you may want to choose to use Prototype in the interest of speeding development even if based solely on the requirements of the project jQuery would be a "better" choice.
My personal opinions
Since you're reading my blog, I will give you what I consider to be my reasonably well-informed personal opinions of the libraries I have worked with and when they make sense:
The library I'm most experienced with is Prototype. The primary reason is that it is the library we have standardized on here at AutoTrader so I work with it extensively on a daily basis. My feelings on Prototype are that it is a great library that provides a lot of great functionality, is for the most part reasonably quick (with some notable exceptions). Both jQuery and MooTools have their roots in Prototype, so moving between those 3 libraries are probably easier than moving to and from others. For general development, my familiarity with Prototype makes it my first choice. The biggest thing Prototype has going for it is its implementation of classes and inheritance easing the transition for those very familiar with object oriented programming.
For everything I like about Prototype, I dislike Script.aculo.us. The way its implemented seems silly to me (creating a new object for each effect is how you execute the effect?) and it doesnt "fit" well with Prototype's code style. It lacks an implementation of Robert Penner's easing equations which make its animations look less impressive.
I have a strong love of jQuery; it's incredibly small, incredibly fast, and very much follows the paradigm of "get out of the way." The plugin architecture is fantastic, and the jQuery UI library is great--fully integrated with the jQuery style of coding and integrates everything I'd expect from a fully-featured animation library. These guys have taken the concept of dereferencing objects to an extreme level--and it can make jQuery code difficult to read at times when you have one line that chains 35 methods. That said, once you understand its power; its actually quite elegant.
MooTools is another great library, and if you're looking to do a lot of heavy animation work, I'd even suggest it over jQuery. MooTools began as an animation extension to Prototype that evolved into its own library--but their heavy focus has always been on providing the fastest, smoothest animations of any library. Obviously its not limited to only animation work; but that's certainly where the bulk of its strength lies.
I'd also like to mention SproutCore--if your goal is to build a fully functional application within a web browser that relies less on Ajax and animation and more on solid application architecture, SproutCore appears to be an excellent choice. I have far less experience with it than other libraries, but from what I've seen, it's MVC implementation is quite impressive!
I'll refrain from in-depth comments on other libraries with which I have no experience--but there are certainly a multitude of choices out there!
Always remember to take your requirements for the library into account on a project-by-project basis--and don't be afraid to work with multiple frameworks. It only makes you that much more valuable to an employer!