最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery構(gòu)造函數(shù)init參數(shù)分析

 更新時(shí)間:2015年05月13日 08:38:34   投稿:hebedich  
這篇文章主要介紹了jQuery構(gòu)造函數(shù)init參數(shù)分析,今天主要是分析一下jQuery中init選擇器構(gòu)造函數(shù),處理選擇器函數(shù)中的參數(shù),感興趣的朋友可以了解下

在我的上一篇隨筆里面分析了jQuery的構(gòu)造函數(shù),jQuery對象中有一個(gè)原型方法init才是是真正的構(gòu)造函數(shù),通過init的原型對象跟jQuery的原型對象保持引用關(guān)系使得init的實(shí)例可以正常調(diào)用jQuery的原型方法,就好像是jQuery的實(shí)例一樣。下面就來看看init這個(gè)幕后的構(gòu)造函數(shù)是怎么寫的:

init: function( selector, context, rootjQuery ) {

...

}

可以看到這個(gè)方法接受3個(gè)參數(shù),其前兩個(gè)參數(shù)是jQuery方法傳遞過來的

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 對象、普通 JavaScript 對象這幾種類型是有效的,這個(gè)參數(shù)是通常是填寫的但是不填寫也不會(huì)報(bào)錯(cuò)

復(fù)制代碼 代碼如下:

console.log($());
//[constructor: function, init: function, selector: "", jquery: "1.7.1", size: function…]

Context作為執(zhí)行上下文或者叫執(zhí)行范圍可以不傳入,或者傳入 DOM 元素、jQuery 對象、普通 JavaScript 對象之一

參數(shù) rootjQuery:包含了 document 對象的 jQuery 對象,用于 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)寫得很清楚了,當(dāng)是這三種情況時(shí)直接return不進(jìn)行任何處理

2.參數(shù) selector 是 DOM 元素

例如: $(document)這種寫法

// Handle $(DOMElement)

if ( selector.nodeType ) {

this.context = this[0] = selector;

this.length = 1;

return this;

}

只要是dom元素肯定有節(jié)點(diǎn)類型,然后把這個(gè)節(jié)點(diǎn)變成jquery對象的第一個(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對象這樣就可以實(shí)現(xiàn)類似$(document).each()這樣的鏈?zhǔn)秸{(diào)用了。最終得到的類似這樣的{0:document,context:document,length:1....}對象,其實(shí)所有的情況最后都會(huì)變成這種形式的對象,除了jQuery原型屬性和方法之外就是獲取的dom節(jié)點(diǎn)并且按照阿拉伯?dāng)?shù)字依次排列,所以我們可以使用$(selector)[0]的形式代替$(selector).get(0)來獲取dom對象。例如:

<!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: div
1: div
2: div
context: document
length: 3
prevObject: jQuery.fn.jQuery.init[1]__proto__: jQuery[0]
selector: "div"
.
*/
  </script>

</html>


3.參數(shù)是特殊的字符串“body”

由于body元素在一個(gè)文檔對象中只有一個(gè)所以單獨(dú)列出來處理

// The body element only exists once, optimize finding it

if ( selector === "body" && !context && document.body ) {

this.context = document;

this[0] = document.body;

this.selector = selector;

this.length = 1;

return this;

}

這里有3個(gè)條件必須同時(shí)滿足,第二個(gè)必須沒有上下文的條件我也不是太理解,$(‘body',document)這樣的看起來很正常的寫法也會(huì)被這種情況“忽視”     

 console.log($('body',document));

 /*

 jQuery.fn.jQuery.init[1]

0: body

context: document

length: 1

prevObject: jQuery.fn.jQuery.init[1]

selector: "body"

__proto__: jQuery[0]

*/

雖然和$('body')的結(jié)果是一樣的,但是卻被當(dāng)做兩種情況來看待,可能是因?yàn)閎ody只有一個(gè)上下文只能是document沒有必要添加吧,否則又要判斷上下文是不是document。第三個(gè)條件是保證document.body必須存在,那么什么情況下會(huì)前兩個(gè)情況滿足document.body又不存在呢?首先就是js代碼先于html代碼加載時(shí)會(huì)出現(xiàn)這個(gè)是初學(xué)者經(jīng)常會(huì)犯的錯(cuò)誤,通常我們要寫成:

$(function(){...})

或者

$(document).ready(function(){...})
 
其實(shí)這兩個(gè)是一樣的調(diào)取的是一個(gè)方法,dom加載這一塊以后在分析。對此我們可以做個(gè)測試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 it

if ( selector === "body" && !context && document.body ) {

this.context = document;

this[0] = document.body;

this.selector = selector;

this.length = 1;

return this;

}

雖然我們只寫了一個(gè)其實(shí)執(zhí)行了四次,只有最后一次才是是我們調(diào)用后的結(jié)果,最后一次的結(jié)果是bodyundefinednull這個(gè)時(shí)候前兩個(gè)就是滿足的但是最后一個(gè)是null。回想起第一篇jQuery總體架構(gòu)架構(gòu)里面undefined會(huì)被重新,那么document.body會(huì)不會(huì)被重寫為null呢?當(dāng)我嘗試在代碼中修改時(shí)就會(huì)報(bào)錯(cuò)看來是不會(huì)的,那這個(gè)條件就是預(yù)防沒有加載html就執(zhí)行的情況吧

第四種是除了上述的字符串情況之外的其他字符串,情況比較多放在下一篇吧。

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評論

德清县| 勃利县| 聂荣县| 吉隆县| 曲松县| 淄博市| 东城区| 封开县| 广东省| 凤翔县| 尼木县| 塘沽区| 霍山县| 惠东县| 河南省| 罗平县| 阿合奇县| 无为县| 昌乐县| 山东省| 武城县| 明溪县| 苍南县| 海伦市| 奉节县| 甘南县| 新竹市| 历史| 嘉善县| 广安市| 贵州省| 增城市| 错那县| 手机| 贵州省| 来安县| 互助| 南岸区| 乌拉特前旗| 化德县| 武乡县|