Why is Susan so complicated?


#1



With my current code, why would this not work? I declared Susan's age to 25. Then, below, I used the setAge method to change her age to 25. If you look in my code where it says "Question", I do not understand why we have to set her age equal to the setAge method. Can someone explain it simply? Thanks!


// here we define our method using "this", before we even introduce bob
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 = new Object();
susan.age = 25;

// ** Question arises here ** : I need a susan.setAge method also equal to setAge. Why?


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


#2

Could you please put the link of the lesson ?


#3

https://www.codecademy.com/courses/spencer-sandbox/2/5?curriculum_id=506324b3a7dffd00020bf661


#4

You didn't write this line:
susan.setAge = setAge;
after


#5

Yes, I know that. If you read my initial thread, you will see that I was not putting that in yet until I understood why it is done. i.e. I know the correct solution, but I want to know exactly why susan.setAge = setAge;
is necessary. Please read line 14 of my code, which is a comment.


#6

Forgive me for not reading carefully,

susan.setAge = setAge; is necessary here, because it means whenever we type susan.setAge( ), this.age
in the setAge method will refer to susan.age.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.