/
examples.js
178 lines (128 loc) · 4.13 KB
/
examples.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* eslint-disable */
<div className="cards">
<div className="card">
<p>I'm a Card</p>
<button>Delete</button>
</div>
<div className="card">
<p>I'm a Card</p>
<button>Delete</button>
</div>
</div>
document
.querySelectorAll('.card button')
.forEach(button => {
button.addEventListener('click', (e) => {
e.currentTarget.closest('.card').remove()
});
})
button.addEventListener('click', (e) => {
// does this button have a class of available and a set data-price attribute?
if(e.currentTarget.matches('.available[data-price]')) {
// .. go!
}
});
/*
returns the closest ancestor which
is a div and has a parent article
*/
el.closest("article > div");
/*
returns the closest ancestor which
is not a div or an article
*/
el.closest(":not(div):not(article)");
/*
Check if a reference to an element in inside another
*/
const modal = document.querySelector('.modal');
modal.contains(button); // true
modal.querySelector('button'); // <button></button>
!!modal.querySelector('button'); // true
Bling.js
window.$ = document.querySelector.bind(document);
window.$$ = document.querySelectorAll.bind(document);
$('.card'); // <div class="card"></div>
$$('.card'); // [<div class="card"></div>,<div class="card"></div>]
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
}
// instead of
document.querySelector('.card').addEventListener('click', handleClick);
// use
$('.card').on('click', handleClick);
NodeList.prototype.__proto__ = Array.prototype;
// Use any array methods on NodeLists
$$('.cards').map(card => card.textContent); // ['card 1','card 2']
NodeList.prototype.on = function (name, fn) {
this.forEach(function (elem, i) {
elem.on(name, fn);
});
}
// instead of:
document
.querySelectorAll('.cards')
.forEach(card => card.addEventListener('click', handleClick));
// use
$$('.cards').on('click', handleClick);
window.$ = document.querySelector.bind(document);
window.$$ = document.querySelectorAll.bind(document);
Node.prototype.on = window.on = function (name, fn) {
this.addEventListener(name, fn);
}
NodeList.prototype.__proto__ = Array.prototype;
NodeList.prototype.on = function (name, fn) {
this.forEach(function (elem, i) {
elem.on(name, fn);
});
}
function getUsers() {
}
getUsers().then(users => {
display(users);
});
// OR
// async function go() {
// const users = await getUsers();
// display(users);
// }
// async function go() {
// const [users, products] = await Promise.all([getUsers(), getProducts()]);
// display(users);
// products(users);
// }
async function go() {
const [users, products] = await Promise.race([getUsers(), getProducts()]);
display(users);
products(users);
}
Array.from({ length: 7 }, (x, index) => (
new Date(0, 0, index).toLocaleDateString('en-US', { weekday: 'long' })
));
const person = {
id: 'abc123',
name: 'Wes',
twitter: '@wesbos'
};
// 1:1 copy
const copyOfWes = { ...wes };
// copy and overwrite
const copyNewId = {
...wes,
id: 'def456'
};
// everything BUT id
const { id, ...withoutId } = wes;
const names = ['wes', 'kait', 'lux', 'poppy', 'snickers'];
const cousins = ['arie', 'ira', 'roman'];
// copy an array
const copy = [...names];
// combine two arrays
const allNames = [...names, ...cousins];
// remove item 3 without mutating the array
const newNames = [...allNames.slice(0,2), ...allNames.slice(3)]
// reverse an array without mutating original
const reversedNames = [...names].reverse();
function useState(intialState) {
let initialState =
}