Are these two approaches functionally the same?
Code:$(function() { $('#menu').css('font-weight', 'bold').fadeIn(); });
What's strange to me is I've tried to do a lot of reading on jQuery chaining/chainability, but I've never been able to find any documentation that discusses what happens when you chain and run functions against an element(s) that doesn't exist?Code:$(function() { // Find the menu. var $menu = $('#menu'); // If the menu wasn't found, get out of here. if (!$menu.length) { return; } // Do the chaining stuff. $menu.css('font-weight', 'bold').fadeIn(); });
So I've been writing a bunch of my code using the latter approach, but I'm wondering my code bloat is unnecessary. Either way doesn't throw errors, but I'm just curious if jQuery is still trying to process the chain on stuff that doesn't exist, making the first approach inefficient.
Thanks.



Reply With Quote


Bookmarks