setAge: setAge. What does that mean?


#1
> var setAge = function (newAge) {
>   this.age = newAge;
> };
> // now we make bob
> var bob = new Object();
> bob.age = 30;
> bob.setAge = setAge;
>   
> // make susan here, and first give her an age of 25
> var susan = {
>     age: 25,
>     setAge: setAge
> };

> // here, update Susan's age to 35 using the method
> susan.setAge(35);
> console.log(susan.age);

*** setAge = setAge. What does that mean ? Can you help me ?


Is naming a var after a property bad practice?
Naming var after prop'ty pt II: naming key after method
#2

Hi
setAge is the function defined in the three first lines of the code. The line bob.setAge = setAge; means that you are adding a property to the object bob. The property name is setAge and is equal to the function written above. Using two times the same name can be confusing. I would suggest to use instead

# The property has the name setMyAge and is equal to the function setAge
bob.setMyAge = setAge;
# You call the function setMyAge of the object bob
bob.setMyAge(45);

For susan, it is the same ambiguity with the names of the property and the function, I would use instead

# definition of susan with the property setMyAge which is equal to the function defined before
var susan = {
     age: 25,
     setMyAge: setAge
};
# call the function setMyAge
susan.setMyAge(35);

I hope it helps


#3

Thank you so much !
It's very good ! :grin: