Weird syntax for extending jQuery

Posted by cantabilesoftware on Stack Overflow See other posts from Stack Overflow or by cantabilesoftware
Published on 2010-06-10T05:27:49Z Indexed on 2010/06/10 5:32 UTC
Read the original article Hit count: 281

Filed under:
|

I recently saw this code on another post ( http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area )

new function($) {
    $.fn.setCursorPosition = function(pos) {
        // function body omitted, not relevant to question
    }
} (jQuery);

After too long trying to understand what it was doing I finally figured out that it's just creating a new function with a parameter $ and then invoking it with jQuery as the parameter value.

So actually, it's just doing this:

jQuery.fn.setCursorPosition = function(pos) {
    // function body omitted, not relevant to question
}

What's the reason for the original, more confusing version?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery