在我的上一篇隨筆里面分析了jQuery的構(gòu)造函數(shù),jQuery對(duì)象中有一個(gè)原型方法init才是是真正的構(gòu)造函數(shù),通過(guò)init的原型對(duì)象跟jQuery的原型對(duì)象保持引用關(guān)系使得init的實(shí)例可以正常調(diào)用jQuery的原型方法,就好像是jQuery的實(shí)例一樣。下面就來(lái)看看init這個(gè)幕后的構(gòu)造函數(shù)是怎么寫(xiě)的:
init: function( selector, context, rootjQuery ) {...}可以看到這個(gè)方法接受3個(gè)參數(shù),其前兩個(gè)參數(shù)是jQuery方法傳遞過(guò)來(lái)的
var jQuery = function( selector, context ) {// The jQuery object is actually just the init constructor 'enhanced'return new jQuery.fn.init( selector, context, rootjQuery );},Selector原則上可以輸入任意值,但并不是所有值都是有意義的,只有undefined、DOM 元素、字符串、函數(shù)、jQuery 對(duì)象、普通 JavaScript 對(duì)象這幾種類(lèi)型是有效的,這個(gè)參數(shù)是通常是填寫(xiě)的但是不填寫(xiě)也不會(huì)報(bào)錯(cuò)
Context作為執(zhí)行上下文或者叫執(zhí)行范圍可以不傳入,或者傳入 DOM 元素、jQuery 對(duì)象、普通 JavaScript 對(duì)象之一
參數(shù) rootjQuery:包含了 document 對(duì)象的 jQuery 對(duì)象,用于 document.getElementById() 查找失敗、selector 是選擇器表達(dá)式且未指定 context、selector 是函數(shù)的情況,其實(shí)就是$(document)。
下面根據(jù)參數(shù)的不同分為12種情況逐個(gè)討論
1.selector 可以轉(zhuǎn)換為false
// Handle $(""), $(null), or $(undefined)if ( !selector ) {return this;}源碼中的注釋已經(jīng)寫(xiě)得很清楚了,當(dāng)是這三種情況時(shí)直接return不進(jìn)行任何處理
2.參數(shù) selector 是 DOM 元素
例如: $(document)這種寫(xiě)法
// Handle $(DOMElement)if ( selector.nodeType ) {this.context = this[0] = selector;this.length = 1;return this;}只要是dom元素肯定有節(jié)點(diǎn)類(lèi)型,然后把這個(gè)節(jié)點(diǎn)變成jquery對(duì)象的第一個(gè)元素并且賦值給上下文context,length屬性是jQuery的原型屬性默認(rèn)為0
// The default length of a jQuery object is 0
length: 0,
這里有了一個(gè)元素之后就把length屬性修改為1。return this 操作使得函數(shù)執(zhí)行后的結(jié)果依然是jQuery對(duì)象這樣就可以實(shí)現(xiàn)類(lèi)似$(document).each()這樣的鏈?zhǔn)秸{(diào)用了。最終得到的類(lèi)似這樣的{0:document,context:document,length:1....}對(duì)象,其實(shí)所有的情況最后都會(huì)變成這種形式的對(duì)象,除了jQuery原型屬性和方法之外就是獲取的dom節(jié)點(diǎn)并且按照阿拉伯?dāng)?shù)字依次排列,所以我們可以使用$(selector)[0]的形式代替$(selector).get(0)來(lái)獲取dom對(duì)象。例如:
<!doctype html><html> <head> <title></title> </head> <body> <div></div> <div></div> <div></div> </body> <script src='jquery-1.7.1.js'></script> <script> console.log($('div'));/*[div, div, div, prevObject: jQuery.fn.jQuery.init[1], context: document, selector: "div", constructor: function, init: function…]0: div1: div2: divcontext: documentlength: 3prevObject: jQuery.fn.jQuery.init[1]__proto__: jQuery[0]selector: "div".*/ </script></html>3.參數(shù)是特殊的字符串“body”
由于body元素在一個(gè)文檔對(duì)象中只有一個(gè)所以單獨(dú)列出來(lái)處理
// The body element only exists once, optimize finding itif ( selector === "body" && !context && document.body ) {this.context = document;this[0] = document.body;this.selector = selector;this.length = 1;return this;}這里有3個(gè)條件必須同時(shí)滿足,第二個(gè)必須沒(méi)有上下文的條件我也不是太理解,$(‘body',document)這樣的看起來(lái)很正常的寫(xiě)法也會(huì)被這種情況“忽視”
console.log($('body',document)); /* jQuery.fn.jQuery.init[1]0: bodycontext: documentlength: 1prevObject: jQuery.fn.jQuery.init[1]selector: "body"__proto__: jQuery[0]*/雖然和$('body')的結(jié)果是一樣的,但是卻被當(dāng)做兩種情況來(lái)看待,可能是因?yàn)閎ody只有一個(gè)上下文只能是document沒(méi)有必要添加吧,否則又要判斷上下文是不是document。第三個(gè)條件是保證document.body必須存在,那么什么情況下會(huì)前兩個(gè)情況滿足document.body又不存在呢?首先就是js代碼先于html代碼加載時(shí)會(huì)出現(xiàn)這個(gè)是初學(xué)者經(jīng)常會(huì)犯的錯(cuò)誤,通常我們要寫(xiě)成:
$(function(){...})
或者
$(document).ready(function(){...})
其實(shí)這兩個(gè)是一樣的調(diào)取的是一個(gè)方法,dom加載這一塊以后在分析。對(duì)此我們可以做個(gè)測(cè)試html代碼如下:
<!doctype html> <html> <head> <title></title> <script src='jquery-1.7.1.js'></script> <script> $('body') </script> </head> <body> <div></div> <div></div> <div></div> </body> </html>然后再jQuery源代碼里面輸出selector、context和document.body
console.log(selector+context+document.body);// The body element only exists once, optimize finding itif ( selector === "body" && !context && document.body ) {this.context = document;this[0] = document.body;this.selector = selector;this.length = 1;return this;}雖然我們只寫(xiě)了一個(gè)其實(shí)執(zhí)行了四次,只有最后一次才是是我們調(diào)用后的結(jié)果,最后一次的結(jié)果是bodyundefinednull這個(gè)時(shí)候前兩個(gè)就是滿足的但是最后一個(gè)是null。回想起第一篇jQuery總體架構(gòu)架構(gòu)里面undefined會(huì)被重新,那么document.body會(huì)不會(huì)被重寫(xiě)為null呢?當(dāng)我嘗試在代碼中修改時(shí)就會(huì)報(bào)錯(cuò)看來(lái)是不會(huì)的,那這個(gè)條件就是預(yù)防沒(méi)有加載html就執(zhí)行的情況吧
第四種是除了上述的字符串情況之外的其他字符串,情況比較多放在下一篇吧。
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注