0

I am coming from this question where I couldn't use this inside the following method:

methods: {
    changeUrl: _.debounce((e) => {
      this.settings.url = e.target.value;
    }, 500),
  },

This threw Uncaught TypeError: Cannot read property 'settings' of undefined.
The answer was that we can't use this inside arrow functions:

An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the this, arguments, super, or new.target keywords.

But then I've noticed that I was using arrow functions inside axios handlers:

    sendData() {
      server
        .post("/endpoint", data)
        .then(res => {
          if (res.status == 200) {
            this.message = "Data was sent";
          } else {
            this.message = "Something went wrong. Please try again";
          }
        })
        .catch(error => {
          this.message = "";
          console.log(error);
        });
    },

And the above code isn't throwing any error and is working perfectly.

Why isn't this.message throwing Uncaught TypeError: Cannot read property 'message' of undefined

What's more, if I change the above code to use normal functions instead of arrow functions:


    sendData() {
      server
        .post("/endpoint", data)
        .then(function(res) {
          if (res.status == 200) {
            this.message = "Data was sent";
          } else {
            this.message = "Something went wrong. Please try again";
          }
        })
        .catch(function(error) {
          this.message = "";
          console.log(error);
        });
    },

this will throw Uncaught TypeError: Cannot read property 'message' of undefined.

Why ?

Jorje12
  • 343
  • 2
  • 10
  • 1
    Because you're calling this within your object instance which has a `.message` property. Arrow functions use the lexical `this` from their definition location, hence why that works for you. Normal functions will use the `this` they are invoked with, which is *not* going to be the same as the context where they are defined. – VLAZ Aug 03 '20 at 19:46
  • Also relevant: [How to access the correct `this` inside a callback?](https://stackoverflow.com/q/20279484) | [How does the “this” keyword work?](https://stackoverflow.com/q/3127429) – VLAZ Aug 03 '20 at 19:47
  • @HereticMonkey I'm glad you linked that as dupe. I was hesitating whether to go with that question or the equivalent / exchangeable one. Ideally both are relevant. – VLAZ Aug 03 '20 at 19:49
  • @VLAZ Yeah, I wish mere mortals could flag multiple duplicate targets. – Heretic Monkey Aug 03 '20 at 19:55
  • 1
    @HereticMonkey I'd love it if silver badge holders were given a bit more leeway like suggesting multiple dupes. Seems like a natural extension to then link multiple dupes explicitly with a gold badge. – VLAZ Aug 03 '20 at 19:57
  • one only needs mention to plob bind on the end of the function `function() { }.bind(this)`, OP wont see the dupes for the trees – Lawrence Cherone Aug 03 '20 at 20:02

0 Answers0