Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Practice Vue.js Templating: Challenge

Sophia Sundance
Sophia Sundance
1,300 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

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,300 Points

Thank you! It is very helpful!

arrow functions do not work with "this" keyword

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