Why is Susan so complicated?


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


Could you please put the link of the lesson ?




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


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.


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.


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