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.
It takes a special kind of human to name variable "name" but not have it be a string.
Named element IDs can be referenced as JavaScript globals
51–60 of 113 posts
Re: Named element IDs can be referenced as JavaScript globals
#52The 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 surpr…
Is there a reason to not use querySelector, since it’s a lot more flexible? One reason jQuery became so popular is because the DOM was painful to use. Things like querySelector fix that.
Re: Named element IDs can be referenced as JavaScript globals
#53This is one of those things that pops up every year or two years. Unfortunately, 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".
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
Re: Named element IDs can be referenced as JavaScript globals
#54Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.
You shouldn't be using IDs anyways. They are just bad for a lot of reasons. You can only have one on a page and it reduces your reusability. Use classes instead.
Re: Named element IDs can be referenced as JavaScript globals
#55I don't want to sound like I have an axe to grind (but I do), but this is the kind of feature/wart that shows the age of the HTML/CSS/JS stack. The whole thing is ripe for a redo. I know they get a lot of hate, but of all the big players in this space I think FB is the best equipped to do this in a way that doesn't ruin everything. I just wonder if they have an incentive (maybe trying to break the Google/MS hegemony…
Re: Named element IDs can be referenced as JavaScript globals
#56Now 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
Re: Named element IDs can be referenced as JavaScript globals
#57It hurts
I think it would hurt less with TypeScript global types. Just need to know what IDs you'd expect to find in the DOM.
Re: Named element IDs can be referenced as JavaScript globals
#58I don't want to sound like I have an axe to grind (but I do), but this is the kind of feature/wart that shows the age of the HTML/CSS/JS stack. The whole thing is ripe for a redo. I know they get a lot of hate, but of all the big players in this space I think FB is the best equipped to do this in a way that doesn't ruin everything. I just wonder if they have an incentive (maybe trying to break the Google/MS hegemony…
Re: Named element IDs can be referenced as JavaScript globals
#59Now I'm worried of using IDs and finding issues with globals in JavaScript. Seems to be a curious issue to be debugged.
You shouldn't be using IDs anyways. They are just bad for a lot of reasons. You can only have one on a page and it reduces your reusability. Use classes instead.
Re: Named element IDs can be referenced as JavaScript globals
#60Earlier quoted context omitted.
Is there a reason to not use querySelector, since it’s a lot more flexible? One reason jQuery became so popular is because the DOM was painful to use. Things like querySelector fix that.
BTW, in my experience getElementById() is still fastest.