|
5 | 5 | * @info:
|
6 | 6 | */
|
7 | 7 | ;(function(win, undefined){
|
8 |
| - var doc = document, |
9 |
| - isIE6 = !win.XMLHttpRequest && !win.opera; |
| 8 | + var doc = win.document, |
| 9 | + loc = win.location, |
| 10 | + docEle = doc.documentElement, |
| 11 | + arr = [], |
| 12 | + slice = arr.slice, |
| 13 | + class2type = {}, |
| 14 | + toString = class2type.toString, |
| 15 | + hasOwn = class2type.hasOwnProperty, |
| 16 | + isIE6 = !win.XMLHttpRequest && !win.opera, |
| 17 | + _ = { // 所有私有方法集合 |
| 18 | + isArraylike : function(obj){ |
| 19 | + var length = obj.length, |
| 20 | + type = $.type(obj); |
| 21 | + |
| 22 | + if(type == 'function' && $.isWindow(obj)){ |
| 23 | + return false; |
| 24 | + } |
| 25 | + |
| 26 | + if(obj.nodeType === 1 && length){ |
| 27 | + return true; |
| 28 | + } |
| 29 | + // http://bbs.csdn.net/topics/390413500 |
| 30 | + // 若type==='array'直接返回true |
| 31 | + // 若type!=='array'的话,如果type!=='function'为true的话开始判断括号里的内容,否则整体返回false |
| 32 | + // 括号里的内容如果length===0为true若括号里整体为true,整体返回true |
| 33 | + // 若length===0为false,判断typeof length==='number',如果为flase,整体返回false |
| 34 | + // 如果typeof length==='number',如果为true,判断length>0,如果为false,整体返回false |
| 35 | + // 如果length>0为true,判断( length - 1 ) in obj,这话的意思就是如果是类数组的对象, |
| 36 | + // 其结构肯定是{0:'aaa',1:'bbb',length:2}这样的key值为数字的,所以如果是类数组对象,判断在obj里是否能找到length-1这样的key,如果找到,整体返回true,否则整体返回false |
| 37 | + // in就是判断一个key是否在一个obj里。比如var obj = {a:'111'},'a' in obj为true,'b' in obj为false |
| 38 | + return type === 'array' || length === 0 || typeof length === 'number' && length > 0 && (length - 1) in obj; |
| 39 | + } |
| 40 | + }; |
10 | 41 |
|
11 | 42 | win.$ = win.Jing = $ = function(selector, context){
|
12 | 43 | return new $.fn.init(selector, context);
|
|
15 | 46 | $.fn = $.prototype = {
|
16 | 47 | version : 1,
|
17 | 48 | constructor : $,
|
18 |
| - splice : [].splice, |
| 49 | + splice : arr.splice, |
19 | 50 | init : function(selector, context){
|
20 | 51 | var obj = null,
|
21 | 52 | context = context || doc,
|
|
94 | 125 | }
|
95 | 126 | return res;
|
96 | 127 | }
|
| 128 | + }, |
| 129 | + each : function(callback){ |
| 130 | + return $.each(this, callback); |
97 | 131 | }
|
98 | 132 | }
|
99 | 133 |
|
|
121 | 155 | return target;
|
122 | 156 | }
|
123 | 157 |
|
124 |
| - var toString = Object.prototype.toString; |
125 |
| - |
| 158 | + |
126 | 159 | // 常用工具函数
|
127 | 160 | $.extend({
|
128 | 161 | isFunction : function(obj){
|
129 |
| - return toString.call(obj) === '[object Function]'; |
| 162 | + return $.type(obj) === 'function'; |
130 | 163 | },
|
131 |
| - isArray : function(obj){ |
132 |
| - return toString.call(obj) === '[object Array]'; |
| 164 | + isArray : Array.isArray || function(obj){ |
| 165 | + return $.type(obj) === 'array'; |
| 166 | + }, |
| 167 | + // 是否为窗口 |
| 168 | + isWindow : function(obj){ |
| 169 | + return obj != null && obj == obj.window; |
133 | 170 | },
|
134 | 171 | trim : function(text){
|
135 | 172 | return text.replace(/^\s+|\s+$/g, '');
|
| 173 | + }, |
| 174 | + type : function(obj){ |
| 175 | + return typeof obj === 'object' || typeof obj === 'function' ? class2type[toString.call(obj)] || 'object' : typeof obj; |
| 176 | + }, |
| 177 | + noop : function(){}, |
| 178 | + each : function(obj, callback){ |
| 179 | + var value, |
| 180 | + i = 0, |
| 181 | + length = obj.length, |
| 182 | + isArray = _.isArraylike(obj); |
| 183 | + |
| 184 | + if(isArray){ // 如果是数组 |
| 185 | + for( ; i < length; i++){ |
| 186 | + value = callback.call(obj[i], i, obj[i]); |
| 187 | + if(value === false){ |
| 188 | + break; |
| 189 | + } |
| 190 | + } |
| 191 | + } else { // 如果不是数组 |
| 192 | + for(i in obj){ |
| 193 | + value = callback.call(obj[i], i, obj[i]); |
| 194 | + if(value === false){ |
| 195 | + break; |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + return obj; |
136 | 201 | }
|
137 | 202 | });
|
138 | 203 |
|
139 | 204 | $.extend({
|
140 | 205 | append : function(){
|
141 |
| - |
| 206 | + return true; |
142 | 207 | }
|
143 | 208 | });
|
144 | 209 |
|
145 |
| - |
| 210 | + $.each('Boolean Number String Function Array Date RegExp Object Error'.split(' '), function(i, name){ |
| 211 | + class2type['[object ' + name + ']'] = name.toLowerCase(); |
| 212 | + }); |
146 | 213 |
|
147 | 214 | }(window));
|
0 commit comments