迷你jq仿造版

/*
*@author enoch  q:512707890
*@create 2012.8
*/
1
(function (global, undefined) { 2 3 var AP = Array.prototype 4 5 function weave(expr, doc) { 6 return new weave.fn.init(expr, doc) 7 } 8 weave.fn = weave.prototype = { 9 construtor: weave, 10 length: 0, 11 splice: [].splice, 12 init: function (expr, doc) { 13 if (!expr) return this 14 return store_result(query(expr, doc), this) 15 16 }, 17 each: function (iFn) { 18 for (var i = 0, j = this.length; i < j; i++) { 19 iFn.call(this[i], i) 20 } 21 }, 22 find: function (expr) { 23 var ret = [] 24 this.each(function () { 25 ret = ret.concat(query(expr, this)) 26 }) 27 return store_result(ret) 28 }, 29 eq: function (start, eqlen) { 30 return store_result(slice(this, start, eqlen)) 31 32 }, 33 bind: function (type, bindFn) { 34 this.each(function () { 35 this.addEventListener(type, bindFn, false) 36 }) 37 }, 38 toString: function () { 39 return slice(this, 0, 0) 40 } 41 } 42 43 weave.fn.init.prototype = weave.fn 44 45 function query(expr, doc) { 46 doc = doc || document 47 return AP.slice.call(doc.querySelectorAll(expr)) 48 } 49 50 function store_result(eles, thisObj) { 51 if (!thisObj) thisObj = new weave 52 thisObj.length = eles.length 53 for (var i = 0; i < eles.length; i++) 54 thisObj[i] = eles[i] 55 return thisObj 56 } 57 58 function slice(arr, index, slicelen) { 59 var ret = [] 60 if (index < 0) { 61 index += arr.length 62 if (index < 0) index = 0 63 } 64 if (!slicelen) { 65 slicelen = 0 === slicelen ? arr.length : 1 66 slicelen += index 67 } else if (slicelen < 0) slicelen += arr.length + 1 68 69 for (var i = index; i < slicelen; i++) 70 ret.push(arr[i]) 71 return ret 72 } 73 74 75 global.$ = weave 76 })(window)
posted on 2012-08-31 12:42  雨弓  阅读(227)  评论(0编辑  收藏  举报