r/javascript • u/legacye • May 03 '10
50 jQuery Snippets That Will Help You Become A Better JavaScript Developer
http://addyosmani.com/blog/50-jquery-snippets-for-developers/3
u/xbudex May 03 '10
Example 5 is wrong for the purpose of optimization. The sizzle engine works by going from right to left, not left to right. The example listed:
var in_stock = $('#shopping_cart_items input.is_in_stock');Will first select all inputs with class 'is_in_stock' and then limit the inputs to the are children of 'shopping_cart_items'.
A better way to do what he was suggesting is to give a context. The example should have been something like this:
var in_stock = $('input.is_in_stock', $('#shopping_cart_items'));1
u/larholm May 05 '10
Providing a context is fine, but it really just adds an additional implicit function call when compared to calling find.
var in_stock = $('#shopping_cart_items').find('input.is_in_stock');1
u/philo23 May 11 '10
You can also do
var in_stock = $('input.is_in_stock', '#shopping_cart_items');Which looks a bit neater and I'd imagine reduces function calls (though I have a feeling jQuery checks for jQuery objects in selectors, still.)
1
u/actionscripted May 04 '10
So many things wrong here. The author should really look into live() and should avoid using browser() stuff as mentioned on the jQuery API page:
We recommend against using this property, please try to use feature detection instead
Also, when did the XCode icon come to represent jQuery?
1
u/philo23 May 11 '10
Some of these snippets are absolutely terrible. Some have good ideas, but the example code is completely wrong (not to mention some are completely broken).
1
3
u/kristopolous May 03 '10
Like, just yesterday: http://www.reddit.com/r/javascript/comments/bz4sf/31_jquery_snippets_that_will_help_make_you_a/ I swear...
Equally poorly written.