Tuesday 23 October 2012

Difference between jQuery.extend and jQuery.fn.extend

During my learning I got some problem in  understanding that what is the difference between $.extend and $.fn.extend and here is what I find,First of all these are two different things.we can understand by an Example :

Use of $.extend :

 

                     var MyObject = {  f1 : function( ) { } }
                    $.extend( MyObject , { f2 : function( ) { }} );

we can see initially MyObject have only one function f1. but by using $.extend we add function f2 with MyObject.
Now MyObject is a object with two function f1 and f2.So we understand that the use of $.extends but what's about $.fn.extends, ok now take another example:

Use of $.fn.extend:

 

                      $.fn.extend({
                                        f1 :  function( ) { },
                                        f2 :  function( ) { }
                    });

Here we have a plugin having two functions f1 and f2. It is very effective when we write multiple functions within a plugin because by using $.fn.extend we doesn't need assign each function separately.  

Anshul.

No comments:

Post a Comment