標籤:
在網上下了一個類似於jQuery的小架構,分析源碼,看看怎麼寫架構。
選取器Select
//用沙箱閉包其整個代碼,只有itcast和I暴漏在全域範圍(function( window , undefined){ //Itcast函數,參數:selector。傳回值一個Itcast對象,功能:new Itcast。prototype。init() function Itcast(selector){ return new Itcast.prototype.init(); } // Itcast的原型屬性用替換重新定義 Itcast.prototype = { constructor: Itcast, type: "Itcast", length: 0, //constructor指向建構函式Itcast,type記錄物件類型“Itcast”,length:變成偽數組 // init函數 功能:Itcast對象的真正的建構函式,根據參數類型,進行不同的處理: 參數:selector。 // 傳回值:只要在適當的位置返回即可,建構函式不需要傳回值,但是為了提醒這裡是為了建立對象,所以寫成return this //因為init是建構函式,this就是指建立出來的這個對象,現在要把擷取到的dom對象全部追加到this中來,再通過Itcast返回 // 。因為Itcast和init是用的同一個原型,所以認為此處建立出來的就是Itcast對象 init:function(selector){ var push = [].push; if( typeof selector == "string"){ // 如果selector是html字串,用parseHtml將其轉成DOM偽數組,再轉成this(Itcast偽數組) if( selector.charAt(0) == "<"){ push.apply(this , parseHTML(selector)); }else{ // 如果selector是選取器。用Select函數, push.apply(this , Select(selector)); } } // 如果是function ,近似於onload if( typeof selector == "function"){ } // 如果是dom,追加到this(Itcast偽數組)中 if( selector.nodeType ){ } // 如果是Itcast對象,追加到新的Itcast對象中返回 if( selector.type = "Itcast" ){ } } } //讓init的原型屬性指向Itcast。prototype。即有init建立出來的對象也就是Itcast對象了 Itcast.prototype.init.prototype = Itcast.fn = Itcast.prototype; //設定Itcast的擴充方式,Itcast。extend(靜態成員擴充) 。 Itcast.prototype.extend(動態成員擴充) // .函數參數: 對象option。 傳回值: 無, 功能:將option中的屬性或方法依次添加給Itcast或Itcast.fn Itcast.fn.extend = Itcast.extend = function(option){ for( var k in option){ this[ k ] = option[ k ]; } } //模組化,功能化。(建構函式的補充) //擴充靜態成員 //選取器Select(閉包,僅將Select暴漏在外面)參數:無,傳回值Select函數。 // 功能:再沙箱中將Select函數定義好, var Select = (function(){ //資料初始化: //將某些瀏覽器是否支援a方法用對象support儲存起來以免每一次動用函數的時候都要進行尋找 // 是否提供可以看函數體是否包含 [native code]即可;,Regex的變數以r開頭 var rnative = /\[native code\]/, push = [].push , i, div = document.createElement("div"); var support = { qsa: rnative.test(document.querySelectorAll), getByClass : rnative.test( document.getElementsByClassName), getByClass2: rnative.test(div.getElementsByClassName), trim: rnative.test(String.prototype.trim), indexOf: rnative.test(Array.prototype.indexOf), } // 要用的函數提前準備好 // push,push沒有相容性問題但是push.apply再IE低版本,不能展開偽數組,會報錯 // ,因此用try{}catch的方法相容 try{ push.apply([],document.getElementsByTagName("*")); }catch(e){ //z自訂push.apply的函數,參數數組a,偽數組b, 傳回值 數組a, // 功能:將b中的內容添加到 a當中; var push = { apply : function( a , b){ for( var i =0 ; i< b.length ; i++ ){ a[ a.length++ ] = b[ i ]; } return a; } } } //trim 參數:字串 ,傳回值:首位去除空白的I字串。功能:將字串的兩端的空白去出,並返回 function trim( str ){ if( support.trim ){ return str.trim(); }else{ var rtrim = /^\s+|\s+$/; return str.replace( rtrim , ""); } } // indexOf,參數數組arr,a要尋找項,傳回值:a的索引值,如果沒有返回-1 //indexOf 還需要一個參數, 就是 尋找的開始位置 function indexOf( arr , a,startIndex){ startIndex = startIndex || 0; if(support.indexOf){ return arr.indexOf(a , startIndex); } for(var i = startIndex; i< arr.length ;i++){ if( arr[ i ] === a){ return i; } } return -1; } //unique函數,參數:數組 ,傳回值:去重之後的數組,功能: 將穿進來的數組中的重複項去除 function unique( arr ){ var array = []; for( var i=0 ; i< arr.length ;i++){ if(indexOf( array ,arr[ i ]) == -1){ array.push( arr[i]); } } return array; } //核心函數Select:參數:selector選取器, 要被添加元素的數組或偽數組results, 傳回值results //功能: 將selector對應的DOM對象,追加到results中輸出 function Select(selector ,results){ results = results || []; if( typeof selector !== "string"){ return ; } //如果瀏覽器支援querySelectorAll就用,如果不支援,就自己定義一個select2 if( support.qsa ){ return document.querySelectorAll( selector ); }else { return select2( selector); } } // select2,參數:selector選取器,傳回值dom對象, function select2( selector ,results){ // 功能:看選取器是不是並集選取器,將selector用trim函數去掉首尾的空白() results = results || []; // ,再用split函數切割成數組, var arr = selector.split(","); for( i = 0 ; i < arr.length ; i++){ // 再用select3函數進行處理,將切割好的字串用trim方法取出前後空白 select3( trim(arr[ i ]) , results); } return unique(results); } function select3 ( selector , results){ results = results || []; //select3函數:參數select ,傳回值dom對象。功能:判斷選取器是“*”還是id,還是類,還是標籤 var first = selector.charAt(0); //注意如果是類別選取器,document.getElementsByClassName有瀏覽器安全色性問題。 //區分好後,調用相應的函數進行處理 //此處屏蔽掉後代選取器的影響,此時如果selector經過了首尾去空格後還有空格,說明是後代選取器 if( selector.slice(" ").length == 1){ if(selector == "*"){ /* results = document.getElementsByTagName("*"); return results; ,如果results原來就有內容,此處把results中的內容全部覆蓋了所以不行, 要將選取器對應的dom元素挨個追加到results中 */ }else if( first == "#"){ return id(selector.slice(1) , results); }else if( first == "."){ //類 return c(selector.slice(1),results); }else { //標籤 return t(selector , results) } }else{ //如果是後代選取器,此處咱不處理,拋出一個bug,提示升級瀏覽器 throw new Error("您的瀏覽器版本過低,請升級瀏覽器") } } //參數:選取器selector 被追加元素的數組。 傳回值: results //功能: 將selector對應的元素挨個追加到results中並輸出, function id( selector ,results ){ results = results || []; push.apply(results , document.getElementById(selector)); return results; } // 標籤 function t( selector , results){ results = results || []; push.apply( results , document.getElementsByTagName( selector)); return results; } //類,getElementsByClassName有瀏覽器安全色行問題, function c( selector , results ){ results = results || []; push.apply( results , getByClass( selector , document)); return results; } //相容類別選取器,參數:類名className ,尋找返回node,傳回值:返回尋找到的dom元素數組 //功能: 用className屬性尋找node中的合格dom對象,並返回對象數組 function getByClass(className , node){ node = node || document; //如果瀏覽器支援 if( node == document && support.getByClass || node.nodeType && support.getByClass2){ return node.getElementsByClassName(className); }else{ //如果瀏覽器不支援 //擷取所有的標籤,遍曆 var list = node.getElementsByTagName("*"), arr = [], tempClassName; for( var i = 0 ; i< list.length ;i++){ tempClassName = list[ i ]; if( !tempClassName ){ continue; } //再class屬性存在的情況下 //用slice(“ ”)分割 //判斷是否與className相同 //如果相同追加到results中 if(indexOf(tempClassName.slice(" "),className) !== -1){ arr.push(tempClassName); } } return arr; } } return Select; })(); // 解析html字串parseHTML //在解析過程中,需要在記憶體中建立一個node,為了不暴漏在外面使用沙箱(自調自用的函數,返回該數組) //參數:selector,(html字串),傳回值儲存dom的數組,功能: 將html字串轉化成dom對象儲存在數組中 Itcast.parseHTML =(function (){ var node = document.createElement("div"); var arr = []; function parseHTML(selector){ node.innerHTML = selector; arr.push.apply(arr , node.childNodes); return arr; } return parseHTML; })(); //擴充執行個體成員 Itcast.fn.extend({ appendTo: function ( parent , child ){ parent.appendChild( child ); } }) //將Itcast掛在window上 window.Itcast = window.I = Itcast;})(window);
分析一個類似於jquery的小架構