3/6 - I don't understand why the "this" keyword doesn't work


#1

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

var printPerson = function(person)
{
console.log(this.firstName + " " + this.lastName);
};

printPerson(contacts[0]);
printPerson(contacts[1]);


I understand using "person.firstName" & "person.lastName" yields the correct answer but shouldn't "this" work as well? I mean I am passing the objects into the function as the parameters am I not?


#2

@vinhcodecade,
Redefine the printPerson function like

var printPerson = function(person) 
{
console.log("== this.printPerson ================");
console.log(this.printPerson.toString());
console.log("== this.mary =======================");
console.log(this.mary);
console.log("== window.mary =====================");
console.log(window.mary);
//console.log(this.firstName + " " + this.lastName);
};

+++ this explained

Reference::

google search
== the Book ==
javascript this site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this
Have a read through
http://stackoverflow.com/questions/4354418/var-vs-this-vs-constructor-parameter-variables
and
http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/


#3

Sorry, I'm still confused. I don't think you addressed the question.


#4

@vinhcodecade,

person is a =local= variable within the FUNCTION-BODY.
So in the FUNCTION-BODY you can only use person.firstName
as you are receiving an object as an argument.

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".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

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".**


#5

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}


#6

person is a =local= variable within the FUNCTION-BODY.
So in the FUNCTION-BODY you can only use person.firstName
as you are receiving an object as an argument.

K thanks, I think it makes a bit more sense now


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.