'This' keyword


#1

I really don't get why we need bob.setAge = setAge;

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


#2

I dont understand this either. The setAge variable is a function and it does and have a value of its own so saying
bob.setAge = setAge does not make any sense to me.


#3

I think I get it now.

The setAge function defined in the beginning is a global function we still need to associate this function with a property on the bob object.
Hence the bob.setAge = setAge is required.

I hope that makes sense.

If you dont have that and simply do
var bob = new Object();
bob.setAge(25);

You will get an error saying setAge is not defined


#4

But why not writing bob.setAge = setAge(); if setAge is a function? i don't get this.