You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the variable name event can hide errors in javascript.
Most browsers (IE, Chrome, and Safari) declare a global window.event before running the handlers, so running your code without declaring the variable could look like it works great. However, Firefox, at least, does not declare the window.event variable, so your code will error in FF.
Examples:
"Bad:"
// Works in all browsers except Firefox$('a').click(function(){
... dosomethingwithevent});// Works in all browsers, but is dangerous, because if you // forget to add `event` to the function declaration, you will // break Firefox and not be given an error$('a').click(function(event){
... dosomethingwithevent});
"Good"
// Works across the board and doesn't cause confusion$('a').click(function(evt){
... dosomethingwithevt});
Recommended solution:
ESLint has 2 rules that can help:
no-restricted-globals
Adding event to this list will prevent the usage of event as a global (i.e. without declaring it)
no-restricted-properties
Adding { object: "event" } to this list will prevent declaring a variable named event.
I recommend that we add at least the first rule to our base config. Adding the second rule is optional, but would avoid ambiguity in our code
The problem:
Using the variable name
event
can hide errors in javascript.Most browsers (IE, Chrome, and Safari) declare a global
window.event
before running the handlers, so running your code without declaring the variable could look like it works great. However, Firefox, at least, does not declare thewindow.event
variable, so your code will error in FF.Examples:
"Bad:"
"Good"
Recommended solution:
ESLint has 2 rules that can help:
no-restricted-globals
event
to this list will prevent the usage ofevent
as a global (i.e. without declaring it)no-restricted-properties
{ object: "event" }
to this list will prevent declaring a variable namedevent
.I recommend that we add at least the first rule to our base config. Adding the second rule is optional, but would avoid ambiguity in our code
References
https://stackoverflow.com/questions/16306486/unexpected-access-to-event-variable-across-browsers
The text was updated successfully, but these errors were encountered: