17 won't work


#1

why won't this work:

// 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 = {
age: 25
};
// here, update Susan's age to 35 using the metho
var setAge = function (newAge) {
this.age = newAge;
};
susan.setAge(35);


#2

it says: Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: susan.setAge is not a function. (In 'susan.setAge(35)', 'susan.setAge' is undefined)


#3

Sir,

I believe the error is cause by the "setAge" function being created twice, you only need one instance of that function and having two will throw you an error, try deleting that and see if your code isn't fixed.


#4

I did not follow the format you used I followed the lesson format so opted to write a constructor when creating susan

// In the editor, we have the same code as last time, where we define setAge using this.

// 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;

We then set bob.setAge = setAge;.

// But this time we will reuse the setAge method for susan as well.

// make susan here, and first give her an age of 25
var susan = new Object();
susan.age = 25;
susan.setAge = setAge;

// here, update Susan's age to 35 using the method

susan.setAge(35);

// here, update Susan's age to 35 using the method

susan.setAge(35);