Question About Instructions


#1

The instructions say the following...

First, remember that
dog.species = dog["species"] = "bulldog";

And if we say:

var x = "species";

then

dog[x] = "bulldog";

what I dont understand is var x = "species";

if you just wrote var x = "species"; would this not just assign the string "species" to the variable x and not the result of calling the property of dog.species? this does not make sense to me. if I just added the line var x = "species"; how would javascript know to pull the string result from species from the dog object without me actually telling it to do so?


#2

@devrockstar84280,

- - - 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
a name property with property-key name and it's associated string VALUE "FIRST"

OR

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

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

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

To create an Object,

you can use the literal notation,

you directly create an Instance of the object, with the
properties being separated by a comma-,

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

OR

You create an Object by the construct notation.

First you create an empty Object by way of either
myObj = new Object(); or myObj = {};
and then you attach its properties using the syntax
object-name.property-key = it's-associated-value ;
( this.name = x ; )

thus:

  var myObj = {};
      myObj.type = 'fancy';
      myObj.disposition = 'sunny';

OR

There is also the facility Class construct notation.

The name should then start with a Capital-letter.

  var Person = function( theName, theAge ) {
         this.name = theName;
         this.age = theAge;
         this.displayInstance = function() {
           console.log("The displayInstance -output-"+
                       "\n============================" +
                     "\n\t name: " + this.name +
                     "\n\t age: " + this.age);
                       };
       };
    //now create an Instance of this Class-object
  var myObj = new Person("Classy_Rocker",20);
    //call the Method displayInstance which takes NO parameters
  myObj.displayInstance();
  console.log( myObj );
  //you can run this in labs.codecademy.com try it !!

As you can see i created a function within this constructor,
they now call this function a Method.
So if in near future the course is asking you to create a method you now know
that you have to create
a property-key with an associated value being a function within an Object.


#3

@devrockstar84280,

- - - 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 and 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"


#4

Thank you for that great and very detailed answer.

So if I get this straight I am actually setting a string value to the variable x, e.g. var x = "species"; but when I am calling for the actual value of the property of dog[x] its actually "bulldog" because it was calling the species property name which I put into the variable x and then it used that string value in the variable that I made to describe the property I wanted when I called it by typing dog[x].

Is this correct? if so, then I think I got it now. If not then I am still confused. :smile:


#5

@devrockstar84280,

because it was calling the species property name which I put into the variable x and then it used that string value in the variable that I made to describe the property I wanted when I called it by typing dog[x].

because it was calling the species property
which I put into the variable x as literal notated "species"
and then it used that string value of the variable that I made
to get access to the associated string Value of the species property
by typing dog[x]


#6

Ok cool thanks. I assume by the look on your face in your picture that your a man not to ask stupid questions to a third time. haha. I appreciate your help on the matter. I learned well from this one. Thanks again.