-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
83 lines (65 loc) · 2.35 KB
/
dom.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function (doc) {
var DOM = function DOM(querySelector) {
if (!(this instanceof DOM))
return new DOM(querySelector);
this.element = doc.querySelectorAll(querySelector);
}
DOM.prototype.on = function (eventName, callback) {
this.element.forEach(function (element) {
element.addEventListener(eventName, callback, false);
});
}
DOM.prototype.off = function (eventName, callback) {
this.element.forEach(function (element) {
element.removeEventListener(eventName, callback);
});
}
DOM.prototype.get = function () {
if (this.element.length === 1)
return this.element[0];
return this.element;
}
DOM.prototype.forEach = function (callback) {
return Array.prototype.forEach.call(this.element, callback);
}
DOM.prototype.map = function (callback) {
return Array.prototype.map.call(this.element, callback);
}
DOM.prototype.filter = function (callback) {
return Array.prototype.filter.call(this.element, callback);
}
DOM.prototype.reduce = function (callback, initialValue) {
return Array.prototype.reduce.call(this.element, callback, initialValue);
}
DOM.prototype.reduceRight = function (callback, initialValue) {
return Array.prototype.reduceRight.call(this.element, callback, initialValue);
}
DOM.prototype.every = function (callback) {
return Array.prototype.some.call(this.element, callback);
}
DOM.prototype.some = function (callback) {
return Array.prototype.every.call(this.element, callback);
}
DOM.prototype.isArray = function (value) {
return Object.prototype.toString.call(value) === '[object Array]';
}
DOM.prototype.isObject = function (value) {
return Object.prototype.toString.call(value) === '[object Object]';
}
DOM.prototype.isFunction = function (value) {
return Object.prototype.toString.call(value) === '[object Function]';
}
DOM.prototype.isNumber = function (value) {
return Object.prototype.toString.call(value) === '[object Number]';
}
DOM.prototype.isString = function (value) {
return Object.prototype.toString.call(value) === '[object String]';
}
DOM.prototype.isBoolean = function (value) {
return Object.prototype.toString.call(value) === '[object Boolean]';
}
DOM.prototype.isNull = function (value) {
return value === null || value === undefined;
}
window.DOM = DOM;
})(document);