My code is changing the bob's age to 50, but its not changing shihab's age to 40.As it is said that "this" function will change based on whatever object we call on?


#1

// 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;
// and down here we just use the method we already made
bob.setAge = setAge;

// change bob's age to 50 here
bob.setAge(50);
var shihab = new Object();
shihab.age = 29;
shihab.setAge(40);


#2

shihab as an object does not have a method called setAge.
You have to do shihab.setAge = setAge;
Then call shihab.setAge


#3

That would be,

shihab.setAge = setAge;
shihab.setAge(40);

We must invoke the method on our object.