-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmakebelieve_example.js
50 lines (38 loc) · 1.25 KB
/
makebelieve_example.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
/* MakeBelieveJS demonstration */
(function () {
function mElem(elem, length) {
this.elem = elem.length === 1 ? elem[0] : elem;
this.length = length;
}
mElem.prototype.nextSibling = function () {
var nextSiblings = [];
for (var i = 0; i < this.length; i++) {
var nextSibling = this.elem[i].nextElementSibling;
if (nextSibling) {
nextSiblings.push(new mElem(nextSibling, 1));
}
}
return nextSiblings;
};
mElem.prototype.previousSibling = function () {
if (this.elem.previousSibling) {
return new mElem(this.elem.previousSibling, 1);
}
return {};
};
var innerMakeBelieve = function(query) {
var elem = document.querySelectorAll(query);
if (elem) {
// The encapsulation object
return new mElem(elem, elem.length);
}
return {};
};
// Attach the outer function to the window object
window.__ = innerMakeBelieve;
})();
// 1. Declare the __ variable and assign to the window object
// 2. Create the basic query handler
// 3. Create the encapsulation object
// 4. Define a method called nextSibling()
// 5. Define a method called previousSibling()