-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyQuery.js
62 lines (55 loc) · 1.32 KB
/
yQuery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(function() {
$ = function(selector) {
if(!(this instanceof $)) {
return new $(selector);
}
var elements;
if(typeof selector === 'string') {
// this returns an array like thing
elements = document.querySelectorAll(selector);
} else if ($.isArray(selector)) {
elements = selector;
}
// need to turn our array like things into an array
[].push.apply(this, elements);
};
var isArrayLike = function(obj) {
if(typeof obj.length === 'number') {
if(obj.length === 0){
return true;
} else if(obj.length > 1) {
return (obj.length-1) in obj;
}
} else {
return false;
}
};
$.extend = function(target, obj) {
for(var item in obj) {
target[item] = obj[item];
}
return target;
};
$.extend($, {
isArray: function(obj) {
return Array.isArray(obj);
},
//https://api.jquery.com/jquery.each/
each: function(obj, callback) {
var result = [];
if(isArrayLike(obj)) {
for(var i = 0; i<obj.length; i++) {
if(callback.call(this, i, obj[i]) === false)
break;
}
} else {
for(var item in obj) {
if(obj.hasOwnProperty(item) === false)
break;
}
}
console.log(result);
return obj;
}
});
}())