jQuery.fn和jQuery.prototype區(qū)別介紹
更新時間:2013年10月05日 15:24:39 作者:
jQuery.fn和jQuery.prototype想必大家對它并不陌生吧,那么你們知道它們之間的區(qū)別嗎?在本文有個不錯的示例大家可以參考下
近期在讀jQuery的源碼。
發(fā)現(xiàn)這里有個東西很難理解。
這里的 jQuery , jQuery.fn , jQuery,fn,init ,jQuery,prototype 都代表什么。
來看下jQuery的源碼是怎么樣定義的:
(function( window, undefined ) {
var jQuery = (function() {
// 構(gòu)建jQuery對象
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
// jQuery對象原型
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
// 合并內(nèi)容到第一個參數(shù)中,后續(xù)大部分功能都通過該函數(shù)擴展
// 通過jQuery.fn.extend擴展的函數(shù),大部分都會調(diào)用通過jQuery.extend擴展的同名函數(shù)
jQuery.extend = jQuery.fn.extend = function() {};
// 在jQuery上擴展靜態(tài)方法
jQuery.extend({
// something to do
});
// 到這里,jQuery對象構(gòu)造完成,后邊的代碼都是對jQuery或jQuery對象的擴展
return jQuery;
})();
window.jQuery = window.$ = jQuery;
})(window);
這里我們可以看到:
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
jQuery 其實jQuery.fn.init()返回一個對象。那么jquery.fn.init()返回的又是什么呢?
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
就是從這里來的,一個javascript對象。
這里有個問題。
jQuery.fn = jQuery.prototype
那么就是 將jQuery 的原型對象賦值給了 jQuery.fn。
再看下面:
jQuery.fn.init.prototype = jQuery.fn;
看到這里我有一個想法。就是 將jQuery.fn 給了 jQuery.fn.init.prototype.
那么在這之前jQuery.fn.init.prototype.是什么?
是不是沒有?這個時候它的原型是{};
那么為了可以去調(diào)用init外面的方法。就做了一個處理。
將jQuery.fn 賦值給jQuery.fn.init.prototype.這樣的話,
jQuery.fn.init.prototype的原型也就是jQuery的原型對象就是 jQuery.fn ( 注意jQuery = function(return new jQuery.fn.init()))。
賦值了以后。在調(diào)用的時候,當(dāng)init中沒有方法的時候,就會去原型函數(shù)中調(diào)用。
那么這樣的話。
你可能會想到這樣一個東東:
jQuery.extends()
jQuery.fn.extends()
我想你應(yīng)該明白它們的區(qū)別了吧。
jQuery.extends()是直接擴展jQuery.而jQuery.fn.extends(),很明顯是擴展的原型。
這就是為什么jQuery.fn.extends()中的大部分方法來自己于jQuery.extends()。
這里又將 jQuery.fn 賦值給了 jQuery.fn.init.prototype.
那么就有這樣的一個關(guān)系:
jQuery.prototype = jQuery.fn = jQuery.fn.init.prototype
發(fā)現(xiàn)這里有個東西很難理解。
這里的 jQuery , jQuery.fn , jQuery,fn,init ,jQuery,prototype 都代表什么。
來看下jQuery的源碼是怎么樣定義的:
復(fù)制代碼 代碼如下:
(function( window, undefined ) {
var jQuery = (function() {
// 構(gòu)建jQuery對象
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
// jQuery對象原型
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
// Give the init function the jQuery prototype for later instantiation
jQuery.fn.init.prototype = jQuery.fn;
// 合并內(nèi)容到第一個參數(shù)中,后續(xù)大部分功能都通過該函數(shù)擴展
// 通過jQuery.fn.extend擴展的函數(shù),大部分都會調(diào)用通過jQuery.extend擴展的同名函數(shù)
jQuery.extend = jQuery.fn.extend = function() {};
// 在jQuery上擴展靜態(tài)方法
jQuery.extend({
// something to do
});
// 到這里,jQuery對象構(gòu)造完成,后邊的代碼都是對jQuery或jQuery對象的擴展
return jQuery;
})();
window.jQuery = window.$ = jQuery;
})(window);
這里我們可以看到:
復(fù)制代碼 代碼如下:
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
jQuery 其實jQuery.fn.init()返回一個對象。那么jquery.fn.init()返回的又是什么呢?
復(fù)制代碼 代碼如下:
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
就是從這里來的,一個javascript對象。
這里有個問題。
復(fù)制代碼 代碼如下:
jQuery.fn = jQuery.prototype
那么就是 將jQuery 的原型對象賦值給了 jQuery.fn。
再看下面:
復(fù)制代碼 代碼如下:
jQuery.fn.init.prototype = jQuery.fn;
看到這里我有一個想法。就是 將jQuery.fn 給了 jQuery.fn.init.prototype.
那么在這之前jQuery.fn.init.prototype.是什么?
是不是沒有?這個時候它的原型是{};
那么為了可以去調(diào)用init外面的方法。就做了一個處理。
將jQuery.fn 賦值給jQuery.fn.init.prototype.這樣的話,
jQuery.fn.init.prototype的原型也就是jQuery的原型對象就是 jQuery.fn ( 注意jQuery = function(return new jQuery.fn.init()))。
賦值了以后。在調(diào)用的時候,當(dāng)init中沒有方法的時候,就會去原型函數(shù)中調(diào)用。
那么這樣的話。
你可能會想到這樣一個東東:
復(fù)制代碼 代碼如下:
jQuery.extends()
jQuery.fn.extends()
我想你應(yīng)該明白它們的區(qū)別了吧。
jQuery.extends()是直接擴展jQuery.而jQuery.fn.extends(),很明顯是擴展的原型。
這就是為什么jQuery.fn.extends()中的大部分方法來自己于jQuery.extends()。
這里又將 jQuery.fn 賦值給了 jQuery.fn.init.prototype.
那么就有這樣的一個關(guān)系:
復(fù)制代碼 代碼如下:
jQuery.prototype = jQuery.fn = jQuery.fn.init.prototype
您可能感興趣的文章:
- js中繼承的幾種用法總結(jié)(apply,call,prototype)
- js中的hasOwnProperty和isPrototypeOf方法使用實例
- js中prototype用法詳細(xì)介紹
- JavaScript prototype 使用介紹
- JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的區(qū)別和應(yīng)用場景簡述
- Javascript中Array.prototype.map()詳解
- JavaScript中的prototype使用說明
- 不錯的一篇關(guān)于javascript-prototype繼承
- 找到了一篇jQuery與Prototype并存的沖突的解決方法
- prototype與__proto__區(qū)別詳細(xì)介紹
相關(guān)文章
用JQuery調(diào)用Session的實現(xiàn)代碼
用JQuery調(diào)用Session的實現(xiàn)代碼,需要的朋友可以參考下。2010-10-10Jquery利用mouseenter和mouseleave實現(xiàn)鼠標(biāo)經(jīng)過彈出層且可以點擊
這篇文章主要介紹了Jquery利用mouseenter和mouseleave實現(xiàn)鼠標(biāo)經(jīng)過彈出層且可以點擊。需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02jquery formValidator插件ajax驗證 內(nèi)容不做任何修改再離開提示錯誤的bug解決方法
query formValidator插件非常好用,但是有一個嚴(yán)重的Bug,在使用ajax驗證的時候,如果輸入框的內(nèi)容已經(jīng)存在,把鼠標(biāo)放到輸入框,不做任何修改再離開,則會提示錯誤,很是郁悶2013-01-01AspNet中使用JQuery上傳插件Uploadify詳解
Uploadify是JQuery的一個上傳插件,實現(xiàn)的效果非常不錯,帶進度顯示。不過官方提供的實例時php版本的,本文將詳細(xì)介紹Uploadify在Aspnet中的使用2015-05-05jquery Deferred 快速解決異步回調(diào)的問題
下面小編就為大家?guī)硪黄猨query Deferred 快速解決異步回調(diào)的問題。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-04-04jquery Mobile入門—多頁面切換示例學(xué)習(xí)
在JQuery Mobile中,多個頁面的切換是通過<a>元素、并將<href>屬性設(shè)置為#+對應(yīng)的id號的方式進行的2013-01-01jQuery選擇器源碼解讀(七):elementMatcher函數(shù)
這篇文章主要介紹了jQuery選擇器源碼解讀(七):elementMatcher函數(shù),本文講解了源碼、功能、參數(shù)、返回函數(shù) 等內(nèi)容,需要的朋友可以參考下2015-03-03