Difference between jQuery.extend and jQuery.fn.extend?

前端 未结 5 707
醉话见心
醉话见心 2020-11-27 09:57

I am trying to understand the jquery plugin syntax, because I want to merge two plugins into one. The blinker that also needs to be able to stop de interval or run a number

5条回答
  •  余生分开走
    2020-11-27 10:31

    Difference between jQuery.extend and jQuery.fn.extend?

    Actually, there is none apart from their base reference. In the jQuery source, you can read:

    jQuery.extend = jQuery.fn.extend = function() { … };
    

    So how does it work? The documentation reads:

    Merges the contents of two or more objects together into the first object.

    It's just a for-in-loop that copies properties, pimped up with a flag to recurse nested objects. And another feature:

    If only one argument is supplied to $.extend(), this means the target argument was omitted

     // then the following will happen:
     target = this;
    

    So if the function is called on jQuery itself (without explicit target), it will extend the jQuery namespace. And if the function is called on jQuery.fn (without explicit target), it will extend the jQuery prototype object where all the (plugin) methods are located.

提交回复
热议问题