Tell us what’s happening:
I do not understand the difference between this.numLegs and dog.numLegs ?
if the value of numLegs would change, it would change wherever it is used, same when using this.
So what’s the difference between them. what’s the utility of this.numLegs ? Your code so far
let dog = {
name: "Spot",
numLegs: 4,
sayLegs: function() {return "This dog has " + this.numLegs + " legs.";}
};
console.log(dog.sayLegs());
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36 Edg/89.0.774.50.
Challenge: Make Code More Reusable with the this Keyword
yes I read it. It i mentioned that this keyword changes the value of properties whenever we change them later in the code.
But as I understand even prop1.val1 will be changed in any place in the code. I think I am understanding something in a wrong way.
In this particular example, this is used as substitute for current object, the method is defined on and helps you change the name of the object without changing all the references to duck.
Now imagine that instead of 3 properties and one method it’ll have 20 properties and 10 complex methods calling each other, using properties etc. and you decide to rename the object from duck to bird. Now you need to go through every method and replace duck with bird. But if you use this regardless of the object’s name it’ll still reference the object itself.
Don’t block yourself if you don’t fully understand this on the first go, it’s a quite complex topic and what you faced in this challenge is the most simplest case.