JavaScript Programmers Can’t Stand on top
The catch of an innocent identifier as global variable
Nov 25, 2020
Please, before you throw anything at me, take a look at the code below.
You can’t stand on top
Chrome: ErrorUncaught TypeError: Cannot assign to read only property ‘top’ of object ‘#<Window>’ …
Firefox: Uncaught TypeError: setting getter-only property “top” <anonymous> …
Standing on bottom is OK
Notes
You may use “top” to name a local variable (declared inside a function), but not to name a global variable (declared outside a function).
But its “inferior” friend, “bottom” you can use as local and as global variable name.
This article was made using
- Chrome version 85.0
- Firefox version 79.0
I had a bad time in the past because Chrome (old version)
- allowed me to declare “top” as global variable
- silently rejected the assignment to global variable “top”
A bad time…