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?


// 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
var shihab = new Object();
shihab.age = 29;


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


That would be,

shihab.setAge = setAge;

We must invoke the method on our object.