开发者

How to create jQuery functions?

开发者 https://www.devze.com 2023-01-28 04:23 出处:网络
I have seen a few different approaches to create jQuery(-namespaced) functions, but I cannot quite tell the actual difference between them.

I have seen a few different approaches to create jQuery(-namespaced) functions, but I cannot quite tell the actual difference between them.

jQuery.fn.myFunction = function() { ... };

jQuery.myFunction =开发者_运维百科 function() { ... };

jQuery.fn.extend({ myFunction: function() { ... } });


jQuery.fn.myFunction is what you use to create functions that will be available on every jQuery result set:

$('div').myFunction();

jQuery.myFunction is what you use to create helper functions that are just available on the jQuery object, such as $.inArray

Your last version extends the $.fn object with a new function, and is as such the functional equivalent of your first example.

0

精彩评论

暂无评论...
验证码 换一张
取 消