0

Im trying to avoid the arrow function because I need to support IE11.

Working code on chrome/firefox/edge:

reader.onload = (e) => {
  let name = this.files[0].name;
  console.log(name);
};

Trying to convert this code into normal function syntax:

reader.onload = function(e) {
  let name = this.files[0].name;
  console.log(name);
};

but now this.files[0] is undefined. In the first code block it isn't undefined. Why is this and how can I fix it?

chazsolo
  • 7,873
  • 1
  • 20
  • 44
Erwin
  • 111
  • 2
  • 11
  • 5
    Could you give a more complete example? What is `reader`? What is `this.files`? Right now there isn't enough information to help. – Calvin Godfrey Jan 10 '19 at 17:09
  • 1
    this will refer to the event here. You will have to explicitly bind this this using bind() method – Monica Acha Jan 10 '19 at 17:13
  • 1
    Then you would use `bind` to bind `this` yourself `(function(e) { ... }).bind(this)`, but for arrow functions you could use babel to transpile your code to an IE11 compatible syntax. – t.niese Jan 10 '19 at 17:16
  • @t.niese Thanks for this answer, works to bind this – Erwin Jan 10 '19 at 22:18

0 Answers0