Live data from Hacker News

Named element IDs can be referenced as JavaScript globals

css-tricks.com

31–40 of 113 posts

Re: Named element IDs can be referenced as JavaScript globals

#31
post #8

Another similar gotcha is that the global-scoped `name` variable must be a string. See https://developer.mozilla.org/en-US/docs/Web/API/Window/name for details. var name = true; typeof name; // "string", not "boolean" Luckily, this is not true within ES modules which you probably use most of the time anymway.

That's not magic, it's just how property getter and setters work on the global:

    
        var _value = "test value";
        Object.defineProperty(window, "testName", {
            get: () => _value,
            set: (value) => { _value = String(value) },
        });
    
    
        var testName = {};
        // prints [object Object] string
        console.log(testName, typeof testName);
        var name = {};
        // prints [object Object] string
        console.log(name, typeof name);
    
the `var` doesn't create a new property since the getter and setter already exist.

Other properties have the same behavior, for example `status`.

Note: there's also LegacyUnforgeable which has similar behavior: https://webidl.spec.whatwg.org/#LegacyUnforgeable

Even if you're not using modules, using an IIFE avoids all this by making your variables local instead of having them define/update properties on the global.

Re: Named element IDs can be referenced as JavaScript globals

#32
post #9
post #5

Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.

Avoid globals at all costs - use IIFE [1] instead, wrapping your function in parenthesis and invoking it right away. [1] https://developer.mozilla.org/en-US/docs/Glossary/IIFE

If you have access to `let`, you can just put `let` declarations into a block. No need for a function to establish scope.

Re: Named element IDs can be referenced as JavaScript globals

#33
post #10

Earlier quoted context omitted.

and then someone always follows up with "Please don't ever do this", without explaining WHY you should never do this: https://wikipedia.org/wiki/Wikipedia:Chesterton's_fence

It's has been explained enough times. It's just that looking things up for yourself seems to have gone out of fashion.

>the person writing about the new discovered weird trick almost always fails to precede the article with a big, red, bold "Please don't ever do this"

> It's has been explained enough times. It's just that looking things up for yourself seems to have gone out of fashion.

It appears you've countered your own complaint.

Re: Named element IDs can be referenced as JavaScript globals

#36
post #6

This has been a thing since the 90s. I really wish we'd done away with it for any document that specifies itself as HTML5. It's great for hacking a tiny script together, however.

Yeah, HTML5 explicitly documented the compatible behaviors between browsers to reach uniformity, which meant standardizing a lot of weird stuff instead of trying to fix it.

See for example this thread where Mozilla tried to not do this: https://bugzilla.mozilla.org/show_bug.cgi?id=622491

Re: Named element IDs can be referenced as JavaScript globals

#38
post #9
post #5

Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.

Avoid globals at all costs - use IIFE [1] instead, wrapping your function in parenthesis and invoking it right away. [1] https://developer.mozilla.org/en-US/docs/Glossary/IIFE

When, today, does it make more sense to organize things around IIFEs and not ES6 modules?

Re: Named element IDs can be referenced as JavaScript globals

#39

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.

`document.all` can be used in this way:

  
  
    const { foo } = document.all
    // do something with foo
  
Don't use it though, it's deprecated as well[1].

[1]: https://developer.mozilla.org/en-US/docs/Web/API/Document/al...

Re: Named element IDs can be referenced as JavaScript globals

#40
The global scope polluter has pretty bad performance and interop surprises, you shouldn't depend on it and instead use getElementById even if it's a bit more verbose.

It uses a property interceptor which is fairly slow in v8:

https://source.chromium.org/chromium/chromium/src/+/main:out...

to call this mess of security checks:

https://source.chromium.org/chromium/chromium/src/+/main:thi...

which has this interop surprise:

https://source.chromium.org/chromium/chromium/src/+/main:thi...

which in the end scans the document one element at a time looking for a match here:

https://source.chromium.org/chromium/chromium/src/+/main:thi...

In contrast getElementById is just a HashMap lookup, only does scanning if there's duplicates for that id, and never surprisingly returns a list!

Post reply on HN