2

Alright, there are 3 cases that I want to talk about. I know that arrow functions have lexical scoping and this will propagate outwards until a match is found. However here are some cases where the behaviours are not making sense to me, so some explanation would be great.

1. Inside a Single File Component's methods

// HelloWorld.vue

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <button @click="nonArrow">click me</button>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  methods: {
    nonArrow() {
      console.log(this);
    },
    arrow: () => {
      console.log(this);
    }
  }
}
</script>

nonArrow() displays VueComponent instance as usual. arrow() displays undefined but shouldn't it display window ?

2. Inside Vue component from script tag.

new Vue({
    el: '#app',
    data: {
    },
    methods: {
        noArrow() {
            console.log(this);
        },
        arrow: () => {
            console.log(this);
        }
})

noArrow() gives a Vue instance, but arrow() gives the window object. Why is window not being referenced in the previous example?

3. Using strict mode

methods: {
    noArrow() {
        console.log(this);
    },
    arrow: () => {
        "use strict";
        console.log(this);
    }
}

In the 2nd example, if I use strict mode with the arrow function inside the methods, it still outputs window object, whereas a normal arrow function defined outside with strict mode will console log undefined. What is going on here?

1 Answer 1

2

Don’t use arrow functions on an options property or callback, such as created: () => console.log(this.a) or vm.$watch('a', newValue => this.myMethod()). Since an arrow function doesn’t have a this, this will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function.

You shouldn't use arrow function. Please refer to this link.

https://v2.vuejs.org/v2/guide/instance.html#Data-and-Methods

Sign up to request clarification or add additional context in comments.

7 Comments

I know I shouldn't use arrow function. I am just trying to understand the reasons for these discrepancies.
Since an arrow function doesn’t have a this, this will be treated as any other variable and lexically looked up through parent scopes until found.
Yes, but where does the lexical scope resolve to in the first example? Why does it show undefined? Which 'this' does it 'match' to?
It basically means that the arrow function takes this from it's context, which means if you try to access this from inside of an arrow function that's on a Vue component, you'll get an error because this doesn't exist!
Okay so what is the context of the arrow function here? Its enclosed inside methods object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.