|
|
|
|
|
by cdrini
1580 days ago
|
|
Oh cool, that's very clever! Probably a little too clever for me, though :P I think the code is misleading, and you can't handle classes with hyphens in it (and if you do, that's even more misleading!). But it's a neat application of proxies! For folks wondering, here's how it would work: const htmlClasses = new Proxy({}, {
get: (target, prop, receiver) => document.querySelector('.' + prop)
});
// Get element with class pagetop
htmlClasses.pagetop;
const htmlNodes = new Proxy({}, {
get: (target, prop, receiver) => document.querySelectorAll(prop)
});
// Get all h3s
htmlNodes.h3;
You can run this on hackernews to see it work. |
|
Anyway, I just think it is cool kind of for the same reason it is misleading :)
You can do a loop like:
and cool is actually a new div each time, even though you would think it was the same cool from divs (divs.cool). It also caches the div, so it is faster than even if you wrote the manual version with createElement.I could get rid of it, because my other approach to element creation would is:
which isn't too bad, but when I have a lot of divs, it is just nicer to do: Also, yeah, it actually creates hyphened classes. so headingContainer goes to .heading-container. That is just how I work and so it is a convention.