Live data from Hacker News

TinyJS – Shorten JavaScript QuerySelect with $ and $$

github.com

61–70 of 94 posts

Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$

#61

Earlier quoted context omitted.

If you like brittle things, the id attribute is already made into an attribute on the window for legacy reasons Edit: My tone may have indicated that parent's solution was brittle. It's not!

The id attribute can take on values that are already present or reserved in window. "fetch", "opener", etc.. The reason to have a separate system that correctly calls getElementById() is to avoid this issue. So, it's actually a _less_ brittle mechanism that doesn't rely on legacy mechanisms and lacks the surprises that come with that.

Sorry, I didn't mean to suggest your solution was brittle -- I actually quite like it and want to adopt it!

But I do think the legacy browser behavior with the ID attribute as window properties is very brittle for the reasons you suggest

Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$

#62

Earlier quoted context omitted.

window['$'] = selector => document.querySelector(selector) window['$$'] = selector => Array.from(document.querySelectorAll(selector)) is really what I want to have

What's the point of the rest of the code? Looks useless.

Looks difficult. I have no clue what I'm reading there tbh.

Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$

#65

I've been using this for years: 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 programming errors set: () => $throw(`Attempt to overwrite id cache key!`) }); It's nice to be able to refer to elements by property name, so: Is reachable with: $id.thing And, since the underlying structure is just an object,…

Element IDs are automatically attached to the window object document.addEventListener('DOMContentLoaded', () => { window.thing.innerHTML = 'Hello, World!'; });

That's (much) slower and has surprising edge cases: https://news.ycombinator.com/item?id=32997636

Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$

#67
post #17
post #2

Like jQuery but for 2024?

That would just be jQuery.

Yep. jQuery 4.0 is in it's second beta[0] and seems to clock in at 27kB. And the "slim" version is [0] https://blog.jquery.com/2024/07/17/second-beta-of-jquery-4-0...

Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$

#69

I've been using this for years: 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 programming errors set: () => $throw(`Attempt to overwrite id cache key!`) }); It's nice to be able to refer to elements by property name, so: Is reachable with: $id.thing And, since the underlying structure is just an object,…

Does caching help? If so, shouldn't getElementById do the same thing under the hood?

Re: TinyJS – Shorten JavaScript QuerySelect with $ and $$

#70

Earlier quoted context omitted.

The id attribute can take on values that are already present or reserved in window. "fetch", "opener", etc.. The reason to have a separate system that correctly calls getElementById() is to avoid this issue. So, it's actually a _less_ brittle mechanism that doesn't rely on legacy mechanisms and lacks the surprises that come with that.

Sorry, I didn't mean to suggest your solution was brittle -- I actually quite like it and want to adopt it! But I do think the legacy browser behavior with the ID attribute as window properties is very brittle for the reasons you suggest

My fault, I tend to "rapid fire" sometimes. Yours was next to another reply on the identical subject and that caused me to mistake the meaning of the comma in your sentence.

Reading it more slowly, I see it now, and, thank you!

Post reply on HN