The "this" Keyword


#1

Ok I know this should be simple but I just can't figure this one out'
here's my code. I am suposed to change bobs age to 50. I did and it posted to the console but I keep get a message to set bobs age to 50

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

// change bob's age to 50 here
bob.setAge = 50;

Anyone help,
cyberzyme


#2

@cyberzyme,

Use this as your code,
maybe it becomes clearer...

var setAge = function (newAge) {
   this.age = newAge;
   };
console.log( "== 1 ==============" );
console.log( setAge );
console.log( setAge.toString() );

console.log( "== 2 ==============" );
// 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;
console.log( bob );
console.log( bob.setAge.toString() );

console.log( "== 3 ==============" );
bob.setAge(50);
console.log( bob );

#3

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**


#4

Thank you, that also clears up a misunderstanding I had with parameters and arguments.