0

I've just stumbled upon something which I think shouldn't be correct.

The code below should not work, as event is not being passed in the args:

var myFunc() {console.log(event.target.value)}

But with arrow functions using Babel to transpile from ES6, the following works

() => {console.log(event.target.value)}

Why should the latter work?

A more specific example can be found at line 43 of this gist, where I'm using react and Babel to transpile ES6 - remove event from the args and it still works as expected.

  • 8
    Those should behave the same (for this example), but you haven't given us a reproducible example, so it's impossible to say what the issue is. – loganfsmyth Aug 31 '16 at 16:16
  • 1
    Why shouldn't the first one work (if it was valid code)? As long as `event` is available in the surrounding scope the function can access it. – a better oliver Aug 31 '16 at 16:31
  • Take this [gist](https://gist.github.com/Samlilli/163c6b5eb776adc311cbb32d9dd73c5f) at line 43, and remove 'event' from the arg - it still works – Sam Lillicrap Aug 31 '16 at 16:51
  • Please trim down the example to a few lines. I cannot run that, so I cannot verify your issue. – loganfsmyth Aug 31 '16 at 17:09

1 Answers1

0

In Internet Explorer (not sure if all versions) events are passed via window.event. So if you use IE when you remove event from arguments list you can still reference global event object.

Edit: Actually it is also available in other browsers. I just tested it in Chrome. I am not sure which browsers exactly support this property.

Michał Młoźniak
  • 5,466
  • 2
  • 22
  • 38