如何创建jQuery函数? [英] How to create jQuery functions?
问题描述
我已经看到了几种创建jQuery(-namespaced)函数的方法,但是我不能确切地说出它们之间的实际区别.
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
是您用来创建将在每个jQuery结果集上都可用的函数的
jQuery.fn.myFunction
is what you use to create functions that will be available on every jQuery result set:
$('div').myFunction();
jQuery.myFunction
是您用来创建仅在jQuery对象上可用的帮助器功能的功能,例如$.inArray
jQuery.myFunction
is what you use to create helper functions that are just available on the jQuery object, such as $.inArray
您的最新版本使用新功能扩展了$.fn
对象,因此与您的第一个示例等效.
Your last version extends the $.fn
object with a new function, and is as such the functional equivalent of your first example.
这篇关于如何创建jQuery函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!