in jQuery what is the difference between $.myFunction and $.fn.myFunction?

后端 未结 3 919
梦谈多话
梦谈多话 2020-12-17 19:14

I\'m delving into writing plugins for jQuery and I\'m trying to understand the distinction between $.f and $.fn.f

I\'ve seen pluggin authors use both, or sometimes a

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 20:00

    The $.f is a utility function whereas $.fn.f is a jQuery plugin / method.

    A utility function is basically a function within the jQuery namespace that is useful for performing some operation, for example, $.isArray(obj) checks if an object obj is an array. It is useful to put functions in the jQuery namespace if you'll use them often and also to avoid global namespace pollution.

    jQuery methods on the other hand operate on jQuery objects/wrapped sets. For example, $(document.body).append('

    Hello

    '); will append a paragraph containing Hello to the body element of the document. $.fn is a shorthand for $.prototype in later versions of jQuery (it wasn't always in earlier versions of the library). You would use this when writing your own plugins.

提交回复
热议问题