自製jQuery(3)

來源:互聯網
上載者:User

標籤:結果   prot   his   end   串連   div   一個   concat   ++   

 1 (function( window, undefined ) { 2     var jQuery = (function(){ 3         var jQuery = function(selector){ 4             return new jQuery.fn.init(selector); 5         }; 6  7         //這裡fn是prototype的別名,後面的代碼擴充fn就是擴充prototype,fn寫起來簡短 8         //如果後面引用了大量的prototype,prototype因為是關鍵字,還不能被壓縮 9         jQuery.fn = jQuery.prototype = {10             constructor: jQuery,11             //這是一個建構函式12             init: function(selector, context){13                 var root;14                 //如果沒有root,就從document開始尋找15                 root = context || document;16                 //17                 var parts = selector.split(" "),                //以空格分離選取器18                     query = parts[0],                           //取出第一段結果19                     //slice返回從第1項到最後一項組成的一個新數組20                     //join把數組中的每個元素轉換為字串,並且用空格串連起來,形成一個String對象21                     rest = parts.slice(1).join(" "),22                     elems = root.getElementsByTagName(query),   //尋找匹配第一段選取器的元素23                     results = [];                               //初始化一個數組,用於儲存查詢結果24 25                     for(var i = 0; i < elems.length; i++){26                         if(rest){27                             //遞迴尋找,以elems[i]為上下文,以rest為選取器運算式28                             //concat會建立一個原數組的副本,並將find的結果添加到results末尾,,這裡又將這個副本的引用存回results29                             results = results.concat(find(rest, elems[i]));30                         }31                         else{32                             //將尋找到的元素儲存在results數組上33                             results.push(elems[i]);34                         }35                     }36                 //真正的jQuery返回的是不是Array對象,是一個類數組對象37                 return results;//返回Array對象38 39             }40         };41         jQuery.fn.init.prototype = jQuery.fn;42         jQuery.extend = jQuery.fn.extend = function(){43             var ob = arguments[0];44             for(var p in ob){45                 if(ob.hasOwnProperty(p) && (!this.hasOwnProperty(p))){46                     this[p]=ob[p];47                 }48             }49         };50 51         //用於擴充全域對象,所以不用jQuery.fn.extend52         jQuery.extend({53             isFunction: function () {54                 if(typeof arguments[0] == "function")55                     return true;56                 else57                     return false;58             }59         });60 61         jQuery.extend({62             isFrom:function(){63             return argument[0].constructor == arguments[1];64             //return arguments[0] instanceof arguments[1];65         }});66 67 68         jQuery.extend({69             each:function(obj,callback){70                 for(var i = 0; i < obj.length; i++){71                     callback.call(obj || null, obj[i], i, obj);72                 }73             }74         });75 /*76  對於HTML文檔,getElementsByTagName返回的是一個HTMLCollection對象,該對象和NodeLIst對象很類似,該函數是用C++實現的77  詳見《JS高程》P25778  return jQuery.makeArray(document.getElementsByTagName(selector));79 */80         jQuery.extend({81             makeArray:function( array, results ){82                 //array是HTMLCollection對像83                 array = Array.prototype.slice.call( array, 0 );84                 if ( results ) {85                     results.push.apply( results, array );86                     return results;87                 }88 89                 return array;90             }91         });92         return jQuery;93     })();94     window.jQuery = window.$ = jQuery;95 })(window);

目前僅支援標籤選取器,如“div", "div p"。而且$("div")返回的是Array對象,和實際的jQuery不同

自製jQuery(3)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.