小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

jQuery.fn和jQuery.prototype區(qū)別介紹

 ThinkTank_引擎 2016-03-10
         近期在讀jquery的源碼。

發(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

您可能感興趣的文章:
jQuery.fn和jQuery.prototype區(qū)別介紹
jquery $.fn $.fx是什么意思有什么用
開發(fā)插件的兩個方法jquery.fn.extend與jquery.extend
通過jQuery源碼學(xué)習(xí)javascript(二)
jQuery中$.fn的用法示例介紹
jquery與prototype框架的詳細對比
jquery的extend和fn.extend的使用說明
jquery中常用的函數(shù)和屬性詳細解析
jQuery 學(xué)習(xí)第七課 擴展jQuery的功能 插件開發(fā)
jQuery基礎(chǔ)框架淺入剖析

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多