JavaScript Practice Vue.js Templating: Challenge

Sophia Sundance
Sophia Sundance
1,266 Points

How can I change the function to a arrow function?

I would like to change the functions in the method to an arrow function, but I did something wrong. It does not work:

methods: {
  increment: function(){
    this.votes += 1; 
  },
  decrement: function(){
    this.votes -= 1; 
  }
}

What I did

methods: {
  increment: () => {
    this.votes += 1; 
  },
  decrement: () => {
    this.votes -= 1; 
  }
}

3 Answers

Heriberto Nieves
Heriberto Nieves
4,514 Points

because arrow functions do not have the "this" keyword, that is why you do not see methods in arrow functions in OOJS. if this sounds unfamiliar do not worry, i had problems wrapping my head around the "this" keyword and arrow functions but basically you cannot use both together it will not work.

watch this, it should explain it more clearly: https://teamtreehouse.com/library/introducing-arrow-function-syntax

for your solution i would use the above

Sophia Sundance
Sophia Sundance
1,266 Points

Thank you! It is very helpful!

Heriberto Nieves
Heriberto Nieves
4,514 Points

arrow functions do not work with "this" keyword

Heriberto Nieves
Heriberto Nieves
4,514 Points

glad i could help, just keep trying and ask if you are stuck. trust me you will eventually will say "Ohhhh" heh