I can't understand what's error I'm making here. Please help

Tell us what’s happening:

Your code so far
I’m getting error in the code below:

1  let dog = {
2    name: "Spot",
3    numLegs: 4,
4    sayLegs: () => { return "This dog has " + this.numLegs + " legs.";}
5  };
7  dog.sayLegs();

Why line no. 4 is not working the same as:

4    sayLegs = function()  { return 'This dog has ' + this.numLegs + ' legs.';}

Challenge: Make Code More Reusable with the this Keyword

Link to the challenge:

look carefully at the punctuation in the sayLegs function

Hi @sam_12-56,

Compare your code to the sample code 's duck object.

Hint: Note that when using arrow functions, this is bound using the enclosing scope where the arrow function is defined.

1 Like

line no.7 (second time) should be:

4    sayLegs: ()  { return 'This dog has ' + this.numLegs + ' legs.';}

=’ should be replaced by ‘:’ .

That means ‘this’ represents the arrow function (sayLegs) and not the dog object.

Thanks brother!

1 Like

Not exactly. Try the following exercise

let dog = {
  name: "Spot",
  numLegs: 4,
  whatsArrowThis: () => console.log(this),
  whatsFuncThis: function () { console.log(this) }


Compare what was logged from both calls. Think about why that is, keeping in mind what I said previously about how this is bound.

That should hopefully solidify within your mind the difference in how this is bound by the different function definition styles.

It also a good idea to deep dive on this topic as much as you can to fully understand it.