jQuery的礦建結構小demo舉例

來源:互聯網
上載者:User

標籤:多個參數   call   屬性   標籤   提高   實現   struct   handle   覆蓋   

    (function (global) {
var document = global.document,//變成局部變數提高搜尋的效能
init;
// 核心函數
function itcast(selector) {
return new itcast.fn.init(selector);
}

// 核心原型--方法庫 儲存所有的方法
itcast.fn = itcast.prototype = {
constructor: itcast,
length: 0//確保時偽數組對象,偽數組沒有元素,但是長度屬性必須有為0 才能稱為偽數組
};
// 建構函式init
/*建構函式,因為函數裡的變數使用者無法擷取到,進行操作,函數可以通過函數名拿到原型,來操作*/
init = itcast.fn.init = function (selector) {
// 處理null undefined
if (!selector) {
return this;
}
// 處理字串
if (itcast.isString(selector)) {
// 處理Html字串解析
if (itcast.isHTML(selector)) {
// 將html字串 轉換成 html元素
// ‘<div><p>123</p></div><div><p>456</p></div>‘
// var o = {} == {0: 1,1: 2,2: 3,length: 3};
// var ar = [1, 2, 3];
// 借調數組的push方法 偽數組儲存資料 格式如{0: 1,1: 2,2: 3,length: 3}; 方便儲存 和使用
Array.prototype.push.apply(this, itcast.parseHTML(selector));
}
// handle: selector
else { //處理選取器
Array.prototype.push.apply(this,
document.querySelectorAll(selector));
}
return this;
}
//處理單個節點
if (itcast.isNode(selector)) {
this[0] = selector;
this.length = 1;
return this;
}
};
//可傳入多個參數的方法
itcast.extend = function () {
var args = arguments;
for (var i = 0, l = args.length; i < l; i++) {
for (var k in args[i]) {
this[k] = args[i][k];
}
}
};
//類型判斷方法 和 工具類方法 是同一個函數,傳如不同的對象,處理的東西不同 因此不會產生覆蓋的現象
// 類型判斷方法 用作過濾
itcast.extend({
isString: function (obj) {//判斷是不是字串
return typeof obj === ‘string‘;
},
isHTML: function (obj) {
//判斷是不是Html字串
/*以 “<” 開頭
長度大於3
以“>” 結尾
滿足以上所有條件才為真
*/
return obj.charAt(0) === ‘<‘ && obj.charAt(obj.length - 1) === ‘>‘ &&
obj.length >= 3;
},
isNode: function (obj) {//判斷是不是dom節點
// !!obj 過濾無效值 並且 節點類型在obj中存在,返回
return !!obj && ‘nodeType‘ in obj;

// return !!obj && obj.nodeType;
}
});
// 工具類方法
itcast.extend({
parseHTML: function (html) {//將Html字串解析方法
//建立div用來裝innerHtml解析的標籤,方便調用innerHTML 屬性,和node = div.firstChild。
var div = document.createElement(‘div‘),
node,
ret = [];//以數組的形式儲存建立的標籤,最後再以偽數組的形式儲存

div.innerHTML = html;
// 遍曆節點
for (node = div.firstChild; node; node = node.nextSibling) {
if (node.nodeType === 1) {
ret.push(node);
}
}
return ret;
},
each: function (obj, callback) {//遍曆方法
var i = 0,
l = obj.length;

for (; i < l; i++) {
if (callback.call(obj[i], i, obj[i]) === false) {
break;
}
}
}
});
// 實現init對象繼承自itcast.prototype
// init對象也 可 被稱為itcast對象
init.prototype = itcast.prototype;

/*將itcast暴露在全域 給使用者使用,必須暴露一個讓使用者操作,
不暴露使用者無法操作,盡量少暴露,越多汙染的可能性就會越大*/
global.$ = global.itcast = itcast;

}(window));

jQuery的礦建結構小demo舉例

聯繫我們

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