jQuery的框架介紹
jQuery使用有一段時(shí)間了,但是有一些API的實(shí)現(xiàn)實(shí)在想不通。小編參考相關(guān)資料源碼,現(xiàn)在把我的學(xué)習(xí)過程和收獲分享給大家。
下面將使用簡(jiǎn)化的代碼來介紹,主要關(guān)注jQuery的實(shí)現(xiàn)思想~>_<~
//匿名立即執(zhí)行函數(shù)
//.防止污染全局空間
//.選擇性保護(hù)內(nèi)部變量
(function(window, undefined){
//第二參數(shù)undefined設(shè)置而不傳的原因:
// 外部發(fā)生這種情況:var undefined = 時(shí),undefined會(huì)被篡改
// 設(shè)置第二參數(shù)而不傳,則undefined就會(huì)被重置回原來值
function jQuery(sel){
return new jQuery.prototype.init(sel);
}
jQuery.prototype = {
constructor: jQuery,
init: function(sel){
if(typeof sel === 'string'){
var that = this;
//jQuery內(nèi)部使用的是Sizzle,這里使用querySelectorAll替代
var nodeList = document.querySelectorAll(sel);
Array.prototype.forEach.call(nodeList, function(val, i){
that[i] = val;
})
this.selector = sel;
this.length = nodeList.length;
}
}
}
jQuery.prototype.init.prototype = jQuery.prototype;
//對(duì)外暴露jQuery:將jQuery綁定在window上面
window.$ = jQuery;
})(window);
--------------------------
jQuery一開始使用匿名立即執(zhí)行函數(shù)包裹其內(nèi)部,并在第5行對(duì)外暴露;
所謂的匿名立即執(zhí)行函數(shù)即這個(gè)函數(shù)是匿名的(沒有名字)、定義完后立即調(diào)用的;
當(dāng)我們?cè)谕獠空{(diào)用$("div")時(shí),其實(shí)調(diào)用的就是內(nèi)部的jQuery("div");
(function(window, undefined){
//內(nèi)部變量
//對(duì)外暴露jQuery:將jQuery綁定在window上面
window.$ = jQuery;
})(window);
$("div")
--------------------------
好,接下來稍復(fù)雜點(diǎn),下面的代碼主要實(shí)現(xiàn)如圖的互相引用:
以$('div')調(diào)用為例:

從第2行代碼可以看出,jQuery使用jQuery.prototype.init來實(shí)例化jQuery對(duì)象,但這會(huì)帶來一個(gè)問題:
實(shí)例化的對(duì)象只能訪問到init下的變量,而不能訪問到j(luò)Query.prototype(jQuery對(duì)外提供的API綁定在該對(duì)象下)。
于是乎,補(bǔ)寫第21行代碼,將init.prototype指向jQuery.prototype即可。
這樣就完成了,使用init來實(shí)例化,且可以在init作用域下訪問到j(luò)Query.prototype。
function jQuery(sel){
return new jQuery.prototype.init(sel);
}
jQuery.prototype = {
constructor: jQuery,
init: function(sel){
if(typeof sel === 'string'){
var that = this;
//jQuery內(nèi)部使用的是Sizzle,這里使用querySelectorAll替代
var nodeList = document.querySelectorAll(sel);
Array.prototype.forEach.call(nodeList, function(val, i){
that[i] = val;
})
this.selector = sel;
this.length = nodeList.length;
}
}
}
jQuery.prototype.init.prototype = jQuery.prototype;
為什么使用jQuery.prototype.init來實(shí)例化對(duì)象,而不直接使用jQuery函數(shù)呢?
假設(shè)使用jQuery函數(shù)來實(shí)例化對(duì)象,這樣對(duì)象之間的引用的確可以簡(jiǎn)化為 jQuery-->jQuery.prototype。
但是調(diào)用會(huì)變得繁瑣起來:new $('div'),所以基于這個(gè)考慮(猜測(cè)(⊙0⊙)),在內(nèi)部使用較為復(fù)雜的實(shí)現(xiàn),來簡(jiǎn)化調(diào)用。
--------------------------
好,最后,再來看一下init的實(shí)現(xiàn)。同樣也簡(jiǎn)化了代碼,只實(shí)現(xiàn)了最常用的一種情況。
jQuery會(huì)把獲取到的nodeList處理成數(shù)組(方便后續(xù)使用),并在其下掛載一些變量,如length,selector。

init: function(sel){
if(typeof sel === 'string'){
var that = this;
//jQuery內(nèi)部使用的是Sizzle,這里使用querySelectorAll替代
var nodeList = document.querySelectorAll(sel);
Array.prototype.forEach.call(nodeList, function(val, i){
that[i] = val;
})
this.selector = sel;
this.length = nodeList.length;
}
}
本文所述到此結(jié)束,下篇文章將給大家介紹jQuery鏈?zhǔn)秸{(diào)用與show知識(shí)淺析,欲了解更多資訊敬請(qǐng)關(guān)注腳本之家網(wǎng)站!
相關(guān)文章
jQuery實(shí)現(xiàn)鎖定頁面元素(表格列)
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)鎖定頁面元素,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
兼容主流瀏覽器的jQuery+CSS 實(shí)現(xiàn)遮罩層的簡(jiǎn)單代碼
比起使用注冊(cè)頁和登陸頁,網(wǎng)站在當(dāng)前頁使用遮罩層注冊(cè)和登陸的用戶體驗(yàn)要好不少。這里使用jQuery和CSS實(shí)現(xiàn)一個(gè)簡(jiǎn)單的遮罩效果。2014-10-10
jQuery實(shí)現(xiàn)對(duì)無序列表的排序功能(附demo源碼下載)
這篇文章主要介紹了jQuery實(shí)現(xiàn)對(duì)無序列表的排序功能,涉及jQuery與javascript常見的文本操作函數(shù)與sort排序函數(shù)的相關(guān)使用方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
jQuery實(shí)現(xiàn)手機(jī)版頁面翻頁效果的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨Query實(shí)現(xiàn)手機(jī)版頁面翻頁效果的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10
使用jQuery+EasyUI實(shí)現(xiàn)CheckBoxTree的級(jí)聯(lián)選中特效
這篇文章主要介紹了使用jQuery+EasyUI實(shí)現(xiàn)CheckBoxTree的級(jí)聯(lián)選中特效的相關(guān)資料,需要的朋友可以參考下2015-12-12
jQuery實(shí)現(xiàn)自定義checkbox和radio樣式
這篇文章主要介紹了jQuery實(shí)現(xiàn)自定義checkbox和radio樣式的相關(guān)資料,需要的朋友可以參考下2015-07-07
判斷多個(gè)input type=file是否有已經(jīng)選擇好文件的代碼
在each中使用return false退出循環(huán),使用return true結(jié)束當(dāng)前次循環(huán),進(jìn)行下一次循環(huán)2012-05-05

