On the difference between jquery.fn.extend and jquery.extend

  • 2020-07-21 06:54:50
  • OfStack

1. jquery. extend (object); To extend the jQuery class itself. Add new methods to the class.
jquery. fn. extend (object); Add methods to the jQuery object.


$.extend({ 
  add:function(a,b){return a+b;} 
}); 

//$.add(3,4);
//return 7 

jQuery adds one "static method" for add, and you can use it where jQuery was introduced.

2. jQuery. fn. extend (object); To extend jQuery.prototype, add "member functions" to the jQuery class. Instances of the jQuery class can use this "member function."


$.fn.extend({ 
  alertClick:function(){ 
    $(this).click(function(){ 
      alert($(this).val()); 
    }); 
  } 
}); 

// On the page: 
<input id="input1" type="text"/>    

// use 
$("#input1").alertClick();  

This is the end of this article, I hope you enjoy it.


Related articles: