3.Displaying People--have a question!


#1

Can someone tell me why can't i code like that??

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


#2

@chancepeng,

Please provide the full-code you are using....

edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

or even better use
= http://discuss.codecademy.com/t/using-backticks-to-format-your-code/3697


#3

sorry,this is the full code

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];

// printPerson added here
function printPerson(person){
console.log(this.firstName + " " + this.lastName);
};
printPerson(contacts[0]);
printPerson(contacts[1]);


#4

@chancepeng,

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


#5

@chancepeng,

google search
+++ this explained
== discussions / opinions ==
javascript this explained site:stackoverflow.com
= http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work

https://www.codecademy.com/forum_questions/55f03d4b93767651db0005b3


#6

@chancepeng,

var mary2 = {
     firstName: "Mary",
     lastName: "Johnson",
     phoneNumber: "(650) 888-8888",
     email: "mary.johnson@example.com",
     printPerson2: function() {
           console.log(this.firstName + " " + this.lastName);
     }
 
}; 

mary2.printPerson2();