Seems like something that could have been made safer just by name spacing it a bit better.
Something like “window.elements.myDiv”? I wonder why the decision to go straight to the root.
Something like “window.elements.myDiv”? I wonder why the decision to go straight to the root.
<div id="foo"></div>
<script>
const { foo } = document.all
// do something with foo
</script>
Don't use it though, it's deprecated as well[1].[1]: https://developer.mozilla.org/en-US/docs/Web/API/Document/al...
// proxy to simplify loading and caching of getElementById calls
const $id = new Proxy({}, {
// get element from cache, or from DOM
get: (tgt, k, r) => (tgt[k] || ((r = document.getElementById(k)) && (tgt[k] = r))),
// prevent overwriting
set: () => $throw(`Attempt to overwrite id cache key!`)
});
Now if you have <div id="something></div>
You can just do $id.something.innerHTML = 'inside!';The simplest possible syntax is to make named elements available globally, and if that clashes with future additions to the DOM API then well that's a problem for some future idiots to worry about.
as a strategy it worked pretty well, unfortunately