0

Given this code:

export default class {
  bar () {
    event.foo()
    arguments.foo()
    foo.foo()
  }
}

When you run the no-undef eslint rule, you get this back:

test_controller.js
  5:5  error  'foo' is not defined  no-undef

foo isn't defined, so that error makes sense.

arguments is implicit (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments) so the lack of error makes sense.

But where is event defined? I'm not aware of it being an implicit variable the way arguments is. I thought this was a bug in no-undef, but I extracted this example from some Stimulus code that works fine without the event parameter. So I want to understand if there's some general JS behavior I'm not aware of here.

For the record (though I'm not sure it matters here), Stimulus calls the function here and does pass an event parameter. But only the name event works "magically" and that's what's really doing my head in. Note that my simplified example doesn't reference Stimulus at all.

Alex Ghiculescu
  • 7,522
  • 3
  • 25
  • 41
  • 2
    It's not implicit, it's global: https://developer.mozilla.org/en-US/docs/Web/API/Window/event (or rather: *was*). – Bergi May 10 '21 at 16:31
  • 2
    [Why is 'event' variable available even when not passed as a parameter?](https://stackoverflow.com/q/33167092) | [Understanding the window.event property and its usage](https://stackoverflow.com/q/6926963) | [What is window.event in JavaScript?](https://stackoverflow.com/q/31544108) | [Javascript events: window.event vs argument reference (function)](https://stackoverflow.com/q/29423624) – VLAZ May 10 '21 at 16:32

0 Answers0