jQuery.fn和jQuery.prototype區(qū)別介紹
更新時(shí)間:2013年10月05日 15:24:39 作者:
jQuery.fn和jQuery.prototype想必大家對(duì)它并不陌生吧,那么你們知道它們之間的區(qū)別嗎?在本文有個(gè)不錯(cuò)的示例大家可以參考下
近期在讀jQuery的源碼。
發(fā)現(xiàn)這里有個(gè)東西很難理解。
這里的 jQuery , jQuery.fn , jQuery,fn,init ,jQuery,prototype 都代表什么。
來看下jQuery的源碼是怎么樣定義的:
(function( window, undefined ) {
var jQuery = (function() {
// 構(gòu)建jQuery對(duì)象
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
// jQuery對(duì)象原型
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)容到第一個(gè)參數(shù)中,后續(xù)大部分功能都通過該函數(shù)擴(kuò)展
// 通過jQuery.fn.extend擴(kuò)展的函數(shù),大部分都會(huì)調(diào)用通過jQuery.extend擴(kuò)展的同名函數(shù)
jQuery.extend = jQuery.fn.extend = function() {};
// 在jQuery上擴(kuò)展靜態(tài)方法
jQuery.extend({
// something to do
});
// 到這里,jQuery對(duì)象構(gòu)造完成,后邊的代碼都是對(duì)jQuery或jQuery對(duì)象的擴(kuò)展
return jQuery;
})();
window.jQuery = window.$ = jQuery;
})(window);
這里我們可以看到:
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
jQuery 其實(shí)jQuery.fn.init()返回一個(gè)對(duì)象。那么jquery.fn.init()返回的又是什么呢?
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
就是從這里來的,一個(gè)javascript對(duì)象。
這里有個(gè)問題。
jQuery.fn = jQuery.prototype
那么就是 將jQuery 的原型對(duì)象賦值給了 jQuery.fn。
再看下面:
jQuery.fn.init.prototype = jQuery.fn;
看到這里我有一個(gè)想法。就是 將jQuery.fn 給了 jQuery.fn.init.prototype.
那么在這之前jQuery.fn.init.prototype.是什么?
是不是沒有?這個(gè)時(shí)候它的原型是{};
那么為了可以去調(diào)用init外面的方法。就做了一個(gè)處理。
將jQuery.fn 賦值給jQuery.fn.init.prototype.這樣的話,
jQuery.fn.init.prototype的原型也就是jQuery的原型對(duì)象就是 jQuery.fn ( 注意jQuery = function(return new jQuery.fn.init()))。
賦值了以后。在調(diào)用的時(shí)候,當(dāng)init中沒有方法的時(shí)候,就會(huì)去原型函數(shù)中調(diào)用。
那么這樣的話。
你可能會(huì)想到這樣一個(gè)東東:
jQuery.extends()
jQuery.fn.extends()
我想你應(yīng)該明白它們的區(qū)別了吧。
jQuery.extends()是直接擴(kuò)展jQuery.而jQuery.fn.extends(),很明顯是擴(kuò)展的原型。
這就是為什么jQuery.fn.extends()中的大部分方法來自己于jQuery.extends()。
這里又將 jQuery.fn 賦值給了 jQuery.fn.init.prototype.
那么就有這樣的一個(gè)關(guān)系:
jQuery.prototype = jQuery.fn = jQuery.fn.init.prototype
發(fā)現(xiàn)這里有個(gè)東西很難理解。
這里的 jQuery , jQuery.fn , jQuery,fn,init ,jQuery,prototype 都代表什么。
來看下jQuery的源碼是怎么樣定義的:
復(fù)制代碼 代碼如下:
(function( window, undefined ) {
var jQuery = (function() {
// 構(gòu)建jQuery對(duì)象
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
// jQuery對(duì)象原型
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)容到第一個(gè)參數(shù)中,后續(xù)大部分功能都通過該函數(shù)擴(kuò)展
// 通過jQuery.fn.extend擴(kuò)展的函數(shù),大部分都會(huì)調(diào)用通過jQuery.extend擴(kuò)展的同名函數(shù)
jQuery.extend = jQuery.fn.extend = function() {};
// 在jQuery上擴(kuò)展靜態(tài)方法
jQuery.extend({
// something to do
});
// 到這里,jQuery對(duì)象構(gòu)造完成,后邊的代碼都是對(duì)jQuery或jQuery對(duì)象的擴(kuò)展
return jQuery;
})();
window.jQuery = window.$ = jQuery;
})(window);
這里我們可以看到:
復(fù)制代碼 代碼如下:
var jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context, rootjQuery );
}
jQuery 其實(shí)jQuery.fn.init()返回一個(gè)對(duì)象。那么jquery.fn.init()返回的又是什么呢?
復(fù)制代碼 代碼如下:
jQuery.fn = jQuery.prototype = {
constructor: jQuery,
init: function( selector, context, rootjQuery ) {
// something to do
}
};
就是從這里來的,一個(gè)javascript對(duì)象。
這里有個(gè)問題。
復(fù)制代碼 代碼如下:
jQuery.fn = jQuery.prototype
那么就是 將jQuery 的原型對(duì)象賦值給了 jQuery.fn。
再看下面:
復(fù)制代碼 代碼如下:
jQuery.fn.init.prototype = jQuery.fn;
看到這里我有一個(gè)想法。就是 將jQuery.fn 給了 jQuery.fn.init.prototype.
那么在這之前jQuery.fn.init.prototype.是什么?
是不是沒有?這個(gè)時(shí)候它的原型是{};
那么為了可以去調(diào)用init外面的方法。就做了一個(gè)處理。
將jQuery.fn 賦值給jQuery.fn.init.prototype.這樣的話,
jQuery.fn.init.prototype的原型也就是jQuery的原型對(duì)象就是 jQuery.fn ( 注意jQuery = function(return new jQuery.fn.init()))。
賦值了以后。在調(diào)用的時(shí)候,當(dāng)init中沒有方法的時(shí)候,就會(huì)去原型函數(shù)中調(diào)用。
那么這樣的話。
你可能會(huì)想到這樣一個(gè)東東:
復(fù)制代碼 代碼如下:
jQuery.extends()
jQuery.fn.extends()
我想你應(yīng)該明白它們的區(qū)別了吧。
jQuery.extends()是直接擴(kuò)展jQuery.而jQuery.fn.extends(),很明顯是擴(kuò)展的原型。
這就是為什么jQuery.fn.extends()中的大部分方法來自己于jQuery.extends()。
這里又將 jQuery.fn 賦值給了 jQuery.fn.init.prototype.
那么就有這樣的一個(gè)關(guān)系:
復(fù)制代碼 代碼如下:
jQuery.prototype = jQuery.fn = jQuery.fn.init.prototype
您可能感興趣的文章:
- js中繼承的幾種用法總結(jié)(apply,call,prototype)
- js中的hasOwnProperty和isPrototypeOf方法使用實(shí)例
- js中prototype用法詳細(xì)介紹
- JavaScript prototype 使用介紹
- JQuery,Extjs,YUI,Prototype,Dojo 等JS框架的區(qū)別和應(yīng)用場(chǎng)景簡(jiǎn)述
- Javascript中Array.prototype.map()詳解
- JavaScript中的prototype使用說明
- 不錯(cuò)的一篇關(guān)于javascript-prototype繼承
- 找到了一篇jQuery與Prototype并存的沖突的解決方法
- prototype與__proto__區(qū)別詳細(xì)介紹
相關(guān)文章
jQuery點(diǎn)擊按鈕彈出遮罩層且內(nèi)容居中特效
這篇文章主要介紹了jQuery點(diǎn)擊按鈕彈出遮罩層且內(nèi)容居中特效,需要的朋友可以參考下2015-12-12
jQuery.position()方法獲取不到值的安全替換方法
這篇文章主要介紹了jQuery.position()方法獲取不到值的安全替換方法,本文給出了一種變通的方法,用.offset()來換算,需要的朋友可以參考下2015-03-03
關(guān)于jquery中attr()和prop()方法的區(qū)別
今兒是腳本之家小編給大家總結(jié)的jquery中attr()和prop()方法的區(qū)別,感興趣的朋友參考下2018-05-05
使用CSS樣式position:fixed水平滾動(dòng)的方法
這篇文章主要介紹了使用CSS樣式position:fixed水平滾動(dòng)的方法,需要的朋友可以參考下2014-02-02
jquery之a(chǎn)jaxfileupload異步上傳插件(附工程代碼)
在處理文件上傳時(shí)需要使用到文件的異步上傳,這里使用Jquery Ajax File Uploader這個(gè)組件,服務(wù)器端采用struts2來處理文件上傳2013-04-04
jQuery html()方法使用不了無法顯示內(nèi)容的問題
jquery中的html方法使用不了,只能用完最基本的innerHTML把內(nèi)容展示出來2014-08-08

