Using jQuery's fn prototype

The jQuery's fn object member is actually an alias for the prototype property of jQuery itself. By means of this object, you can extend jQuery by adding new methods to the jQuery's chain. For example:

jQuery.fn.insert = function(element, content, attribute, value) {

    element = element || '<div></div>';
    
    content = content || 'Test';
    
    attribute = attribute || 'class';
    
    value = value || 'test';
    
    return $(element).appendTo(this).html(content).attr(attribute, value);
}


$(document).ready(function() {

     
      $('body').insert();
      
      

});

The problem with jQuery's prototype is that it's not extensible from other classes except from jQuery itself. In other words, the link to the prototype property works as expected, but your new class cannot use the core jQuery's methods. You can see the test of the above example here.

This entry was posted in by Gabriele Romanato. Bookmark the permalink.

Leave a Reply

Note: Only a member of this blog may post a comment.