jQuery源碼筆記——三

來源:互聯網
上載者:User

標籤:style   blog   io   color   ar   java   for   sp   div   

將類數組對象轉化為數組對象

javascript中有許多類數組對象,比如HTMLCollection,NodeList,arguments。她們的特點是和數組一樣有length屬性,並且有0,1,2這樣的位置屬性。在代碼編寫中我們經常需要將他們轉化為數組對象。

//mini類數組對象var arrayLike = {    0: "a",    1: "b",    2: "c",    length: 3}console.log(Array.prototype.slice.call(arrayLike))

我們來詳細分析一下Array.prototype.slice.call(arrayLike)。數組的中有slice方法,存放在數組的原型中也就是Array.prototype.slice,它操作的傳回值是一個數組;call具有修改內容相關的作用,本例就是將slice的上下文改為arrayLike。所以這句話就實現了將類數組對象轉化為數組對象的功能。

.get()實現
var jQuery = function( selector, context ) {        return new jQuery.fn.init( selector, context );};jQuery.fn = jQuery.prototype = {    init: function(selector){        this.selector = selector;        //從IE8之後提供了querySelectorAll,我們先利用它mini化選取器        //返回的是偽數組對象NodeList        var result = document.querySelectorAll(selector);        //將NodeList轉化為jQuery對象。        for(var i = 0;i < result.length;i++){            this[i] = result[i]         }        //類比jQuery對象的length屬性        this.length = result.length;    }}jQuery.fn.init.prototype = jQuery.fn//不利用extend ,直接向原型裡添加屬性方法getjQuery.fn.get = function(index) {        return index != null ? (index < 0 ? this[this.length + index]:this[index]):Array.prototype.slice.call(this);}//測試console.log(jQuery("div").get(0))

.get的作用是轉化為DOM節點,或者是DOM節點數組。注意一些特別的情況。

.eq()實現
var jQuery = function( selector, context ) {        return new jQuery.fn.init( selector, context );};jQuery.fn = jQuery.prototype = {    selector: "",    init: function(selector){        //仍然是mini的選取器。        var result = document.querySelectorAll(selector);        for(var i = 0;i < result.length;i++){            this[i] = result[i]         }        this.length = result.length;    },    pushStack: function( elems ) {        //將空jQuery對象和elems合并        var ret = jQuery.merge( this.constructor(), elems );        //設定前一個對象,作回來的索引。        ret.prevObject = this;        // 新形成jQuery的對象        return ret;    },    eq : function( i ) {        //將負數位置轉化為正數位置        var j = + i + ( i < 0 ? this.length:0);        //在範圍內返回指定對象包成的數組,否則返回空數組        return this.pushStack((j >= 0&& j<this.length)?[this[j]]:[])    },    //將原型的建構函式設定為jQuery,可以用jQuery.constructor創造新的Null 物件;相關語句this.constructor;jQuery.prototype;    constructor : jQuery}jQuery.fn.init.prototype = jQuery.fn//將兩個類數組對象或數組對象合并,並設定,lengthjQuery.merge = function( first, second ) {        var len = +second.length,            j = 0,            i = first.length;        for ( ; j < len; j++ ) {            first[ i++ ] = second[ j ];        }        first.length = i;        return first;};//測試console.log(jQuery("div").eq(0))

先講兩個相關函數jQuery.fn.pushStatic和jQuery.merge。

寫在jQuery.fn裡的函數實際寫在原型裡,用到這個原型的建構函式都會繼承;而寫在jQuery裡的實際是在一個獨立的jQuery對象,只能通過jQuery.[函數名]的形式引用。

merge實際的功能是合并兩個數組或者類數組對象,放到第一個對象中,並設定他們合并後的長度。pushStatic調用了merge,第一個參數為jQueryNull 物件(this.constructor()),將第二個數組或者類數組合并到jQueryNull 物件中,形成新的jQuery對象,並返回。

eq的功能就是get的功能多一個轉化成jQuery對象,調用pushStatic,並返回其傳回值一個新的jQuery對象。

first和last和end
  first: function() {        return this.eq( 0 );    },    last: function() {        return this.eq( -1 );    },    end: function() {        return this.prevObject || this.constructor(null);    },

jQuery源碼筆記——三

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.