Literally Speaking


#1



https://www.codecademy.com/en/courses/objects-ii/0/3?curriculum_id=506324b3a7dffd00020bf661#


Okay, so my syntax is correct. My question isn't regarding code, but the application of the code. For the function speak, why is it necessary that you add a parameter, instead of just using the name of the actual function in the "console.log" statement?

I've noticed that sometimes the lessons require the use of a parameter, and sometimes they don't. Perhaps I just need to review functions in general, but when is it necessary to use a parameter?

Thanks in advance for your help!


var james = {
    job: "programmer",
    married: false,
    speak: function(dog ) {
       console.log("Hello, I am feeling " + dog); 
    }
};

james.speak("great");
james.speak("just okay");


#2

Adding a parameter makes you be able to do this,

And it is advisable to make your parameters related to what the code is doing for you to understand it better. For example naming the parameter adjective or feeling or even emotion makes you comprehend the code better.


#3

@megsen,

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

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#4

@megsen,

james job married explained

- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it's associated value.

 var object1 = {
              name: "First"
              }

So object1 has 1 property with
a type property-key name
and it's associated value "FIRST" (which in this case is a "string-value")
OR

var myObj = {
         type: 'fancy',
         disposition: 'sunny'
        }

myObj has 2 properties seperated by a comma-,,
a property with property-key type and an associated-string-value 'fancy'
a disposition-property with property-key disposition and
..an associated-string-value 'sunny'.

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

var james = {
    job: "programmer",
   married: false,
   sayJob: function() {
          // complete this method
          console.log("Hi, I work as a" + this.job);
    }
 };

Description of the james object.
The james object has 3 properties which are seperated by a comma-,

there is a job property with property-key job and it's associated string value 'programmer'
there is a married property with property-key married and it's associated boolean value false
there is a sayJob property with property-key sayJob and it's associated anonymous function VALUE
( they also would 'say', the james-object has the sayJob()-Method )

access via dot-notation

james.job ==> you will get the associated string VALUE of the job property-key, thus
you get the string VALUE 'programmer'

james.married ==> you will get the associated boolean VALUE false

james.sayJob ==> you will get the associated anonymous function VALUE
to call/execute this method you add a pair of parenthesis-( )
james.sayJob();

access via the square-bracket-notation

1 using the literal property-key

james["job"]
james["married"]
james["sayJob"]()

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "job";
var propertyKey2 = "married";
var propertyKey3 = "sayJob";
james[propertyKey1] ==> you will get the associated string VALUE of the job property-key, thus
you get the string "programmer"

james[propertyKey2]
in this case you will get the boolean VALUE of the married property
being false

james[propertyKey3]();
would =display= "Hi, I work as a programmer"

+++++++ this explanation +++++++++++
I defined an extra property in the james object.
in this case a method
as there is a property-key sayJob2
with it's associated value being a function...

var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
              // no PARAMETER used
              // job-Value is picked up from within this Object
              console.log("Hi, I work as a" + " " + this.job);
    },
    sayJob2: function(job) {
              // 1 PARAMETER is used as =local= VARIABLE
              // job will get the  Value as you call providing 1 ARGUMENT
              console.log("Hi, I work as a" + " " + job);
    }
 
 };

 james.sayJob();

 james.sayJob2("xx");

An object has one or more properties separated by a comma-,
A property consists of a property-key and it's associated value.
The associated value can be anything
from an Array [] , null, "string", number, true , _an Object {} or a function.

If the associated value of a property-key is a function
we are calling this property a METHOD.
(if in the course they ask you to create a method you will now know that
you have to create a function within an object using the function-name
as property-key)

Normally if you are using the this keyword in a Method,
you are referring to the property of the object of which it is part of.

So in our case:
james.sayJob();
would translate to
console.log("Hi, I work as a" + " " + "programmer");

A good article about this:
http://stackoverflow.com/questions/80084/in-javascript-why-is-the-this-operator-inconsistent


#5

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