在jQuery中$ .myFunction和$ .fn.myFunction有什么区别? [英] in jQuery what is the difference between $.myFunction and $.fn.myFunction?

查看:131
本文介绍了在jQuery中$ .myFunction和$ .fn.myFunction有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究为jQuery编写插件,并且试图理解$ .f和$ .fn.f之间的区别

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

我已经看到插件作者同时使用这两者,或者有时分配$ .f = $ .fn.f

I've seen pluggin authors use both, or sometimes assign $.f = $.fn.f

有人可以向我解释这一点,推理,好处等吗?

Can someone explain this to me, reasoning, benefits, etc?

推荐答案

查看jQuery源代码将清除所有内容.顺便说一句,jQuery$引用相同的对象,这就是jQuery对象的定义方式:

Looking at the jQuery source code will clear things up. By the way, jQuery and $ refer to the same object, and this is how the jQuery object is defined:

var jQuery = function( selector, context ) {
    return new jQuery.fn.init( selector, context );
}

jQuery是一个函数,在Javascript中,函数也是Function类型的对象.因此,jQuery.f$.ff附加到jQuery Function对象,或者,如果可以的话,将其称为jQuery类.

jQuery is a function and in Javascript, a function is also an object of the type Function. So jQuery.f or $.f attaches f to the jQuery Function object, or call it the jQuery class if you will.

如果您查看jQuery的源代码,您会发现jQuery.prototype已分配给jQuery.fn

if you look at jQuery's source, you'll see that jQuery.prototype has been assigned to jQuery.fn

jQuery.fn = jQuery.prototype

因此,每当您将方法(或属性)附加到jQuery.fn时(如jQuery.fn.f = ..$.fn.f = ..jQuery.prototype.f = ..$.prototype.f = ..一样),该方法将可用于此方法的所有实例jQuery类(同样,Javascript中没有类,但可能有助于理解).

So, whenever you attach a method (or property) to jQuery.fn, as in jQuery.fn.f = .., or $.fn.f = .., or jQuery.prototype.f = .., or $.prototype.f = .., that method will be available to all instances of this jQuery class (again, there are no classes in Javascript, but it may help in understanding).

无论何时调用jQuery()函数(如jQuery("#someID")一样),都会在此行上创建一个新的jQuery实例:

Whenever you invoke the jQuery() function, as in jQuery("#someID"), a new jQuery instance is created on this line:

return new jQuery.fn.init( selector, context );

,此实例具有我们附加到原型的所有方法,但没有直接附加到Function对象的方法.

and this instance has all the methods we attached to the prototype, but not the methods that were attached directly to the Function object.

如果尝试调用未在正确位置定义的函数,则会出现异常.

You will get an exception if you try calling a function that wasn't defined at the right place.

$.doNothing = function() {
    // oh noez, i do nuttin
}

// does exactly as advertised, nothing    
$.doNothing();

var jQueryEnhancedObjectOnSteroids = $("body");
// Uncaught TypeError: Object #<an Object> has no method 'doNothing'
jQueryEnhancedObjectOnSteroids.doNothing();


哦,最后要缩短一条长螺纹并回答您的问题-进行$.f = $.fn.f允许您将该函数用作插件或实用程序方法(在jquery术语中).


Oh, and finally to cut a long thread short and to answer your question - doing $.f = $.fn.f allows you to use the function as a plugin or a utility method (in jquery lingo).

这篇关于在jQuery中$ .myFunction和$ .fn.myFunction有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆