Way to break jQuery
Way to break jQuery
Hey guys, wanted to let you know how I accidently broke jQuery:
<blockquote>var fn = function(selector) {
selector = selector || '.myClassName';
jQuery(selector).addClass('someClass');
}
jQuery(document).ready(fn);
</blockquote>
I wanted to create a function, fn, that could take a selector but when none were supplied it would affect (in this case) all elements with "myClassName" as a class. What happens is that when you call jQuery(document).ready(fn), the ready() function supplies $ or jQuery as a parameter to all functions in the ready list.
<span style="font-weight: bold;">This is bad!</span>
Primarily because it's not expected. What this does is actually break addClass for jQuery from this line on and it doesn't throw an error either. You can replicate this by doing
jQuery(jQuery).addClass('someClass'); alert(jQuery.className)
It overwrites the function. Of course I would never intentionally do jQuery(jQuery) but that is the result in the case above.
--
Sincerely,
Gabriel Harrison
(if you encounter issues with my gmail account try me at <a href="mailto:nyteschayde@yahoo.com">nyteschayde@yahoo.com</a>)