The difference between jQuery.extend() and jQuery.fn.extend()


/ Published in: jQuery
Save to your folder(s)

The difference between jQuery.extend() and jQuery.fn.extend()


Copy this code and paste it in your HTML
  1. $.fn.extend({
  2. myMethod: function(){...}
  3. });
  4. //jQuery("div").myMethod();
  5.  
  6. $.extend({
  7. myMethod2: function(){...}
  8. });
  9. //jQuery.myMethod2();
  10.  
  11. defaults = { size: 3 };
  12. options = { height: 6 };
  13. var opts = $.extend(defaults, options)
  14. // 'defaults' receives the methods and variables defined in 'options'
  15. // opts == defaults == { size: 3, height: 6 }
  16. // options == { height: 6 };
  17.  
  18. <pre>var opts = $.extend( {}, defaults, options)
  19. // 'opts' gets all methods and variables defined in 'defaults' and 'options',
  20. // neither of them get modified.
  21. // opts == { size: 3, height: 6 }
  22. // defaults == { size: 3 };
  23. // options == { height: 6 };

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.