Skip to content

Commit 0d40877

Browse files
committed
test
1 parent 665fa90 commit 0d40877

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

js/jing.js

+77-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,39 @@
55
* @info:
66
*/
77
;(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+
};
1041

1142
win.$ = win.Jing = $ = function(selector, context){
1243
return new $.fn.init(selector, context);
@@ -15,7 +46,7 @@
1546
$.fn = $.prototype = {
1647
version : 1,
1748
constructor : $,
18-
splice : [].splice,
49+
splice : arr.splice,
1950
init : function(selector, context){
2051
var obj = null,
2152
context = context || doc,
@@ -94,6 +125,9 @@
94125
}
95126
return res;
96127
}
128+
},
129+
each : function(callback){
130+
return $.each(this, callback);
97131
}
98132
}
99133

@@ -121,27 +155,60 @@
121155
return target;
122156
}
123157

124-
var toString = Object.prototype.toString;
125-
158+
126159
// 常用工具函数
127160
$.extend({
128161
isFunction : function(obj){
129-
return toString.call(obj) === '[object Function]';
162+
return $.type(obj) === 'function';
130163
},
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;
133170
},
134171
trim : function(text){
135172
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;
136201
}
137202
});
138203

139204
$.extend({
140205
append : function(){
141-
206+
return true;
142207
}
143208
});
144209

145-
210+
$.each('Boolean Number String Function Array Date RegExp Object Error'.split(' '), function(i, name){
211+
class2type['[object ' + name + ']'] = name.toLowerCase();
212+
});
146213

147214
}(window));

test.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
// var test = Jing('.a');
1717
// var test = $(document.body);
1818
// var test = $('<p><span>test</span></p>');
19-
var test = $('<p>test</p>');
19+
// var test = $('<p>test</p>');
2020
// var test = $(document);
21-
console.log(test);
21+
// console.log(test);
22+
$.each([1,2,3], function(i, n){
23+
console.log(i + ' : ' + n);
24+
});
2225

2326
/*
2427
Jing.extend({

0 commit comments

Comments
 (0)