5. Do you have the variable 'aProperty' between the brackets? Unsure how to fix this since from I can see I do


#1

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = james["job"];

// print the value of the first property of "james"
// using the variable "aProperty"
console.log(aProperty);


#2

@brian_marmon,

- - - 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 repl.it 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

@brian_marmon,

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

OMG! ^^^ Simple answer:
var aProperty = 'job';
console.log(james[aProperty]);


#5

What if you just wanted to save an arbitrary string value that also happened to be "job"? Do property-key's always override simple strings when referenced in a variable? How do we avoid this conflict?


#6

@gendakel
I do not understand the question....
could give an example....


#7

var james = {
    job: "programmer",
    married: false
};

// set to the first property name of "james"
var aProperty = "job";

// print the value of the first property of "james" 
// using the variable "aProperty"
console.log(james[aProperty]);

So in the above example, var aProperty is a global property correct? It is referencing the property-key "job" within the james object. Let's say I just needed to save the word "job", as a simple string value, to another variable as such:

var myLeastFavoriteThingToDoIs = "job";

It seems like that won't work because it will continue to save the property-key, as in the case of "var aProperty". It seems confusing to me that you can reference a property-key just by wrapping it with quotes, because that property-key may be a common word that may be needed as a simple string elsewhere.

^ Unless that is exactly the point, that you should not be using common words as property-key's in case you need to save that as a string elsewhere in your code?

Hope I am making sense.


#8

@gendakel
Keep repeating / reading
5. Do you have the variable 'aProperty' between the brackets? Unsure how to fix this since from I can see I do

=== the Objects 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
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 repl.it 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.

##Reference
== the Book ==


== discussions / opinions ==
http://stackoverflow.com/questions/1744426/does-javascripts-new-operator-do-anything-but-make-life-difficult
http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript
http://stackoverflow.com/questions/4869712/new-without-delete-on-same-variable-in-javascript
== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html <---JavaScript code-convention
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript
www.developer.mozilla.org/en-US/docs/Web/JavaScript/Guide

###==============================================

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it’s associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,

  • a fullName property with property-key fullName and it’s associated string value of “New York City”
  • a mayor property with property-key mayor and it’s associated string value of “Bill de Blasio”
  • a population property with property-key population and it’s associated number value 8000000
  • a boroughs property with property-key boroughs and it’s associated number value 5
for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration’s
iteration-1 var x = “fullName”;
iteration-2 var x = “mayor”;
iteration-3 var x = “population”;
iteration-4 var x = “boroughs”;
As they want you to display the property-key and NOT it’s associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );

###========================================================

An object has one or more properties, separated by a comma_,

A property has a property-key and it’s associated VALUE

The associated VALUE can be anything from:

•Number
•String
•Boolean
•Symbol
•Object  ◦Function/'Method'
         ◦Array
         ◦Date
         ◦RegExp
         ◦Math
•Null
•Undefined

Above you have an overview of all data-types.

###=============================================================
now to your case:
var aProperty = “job”;
console.log( typeof aProperty );
will leave you with a variable which has a VALUE of the string-type

As the string is created, you will get a present…the length Property
prototypeType.hasOwnProperty(“length”) ==> true.

From the String.prototype you will inherit the methods:
‘sup’,
‘sub’,
‘strike’,
‘small’,
‘link’,
‘italics’,
‘fontsize’,
‘fontcolor’,
‘fixed’,
‘bold’,
‘blink’,
‘big’,
‘anchor’,
‘valueOf’,
‘trim’,
‘toUpperCase’,
‘toString’,
‘toLowerCase’,
‘toLocaleUpperCase’,
‘toLocaleLowerCase’,
‘substr’,
‘substring’,
‘split’,
‘match’,
‘localeCompare’,
‘concat’,
‘charCodeAt’,
‘charAt’,
‘slice’,
‘search’,
‘replace’,
‘lastIndexOf’,
‘indexOf’

And as anything in the Javascript environment is an object
from the Object.prototype you inherit:
’__lookupSetter__’,
’__lookupGetter__’,
’__defineSetter__’,
’__defineGetter__’,
’__proto__’,
‘toLocaleString’,
‘isPrototypeOf’,
‘propertyIsEnumerable’,
‘hasOwnProperty’ \<== this is a special one !!

Have a look at


and look in the left-hand column !!!
properties & methods ( =predefined functions with their specific syntax !! )


#9

code is simple, here is the correct format for you. @brian_marmon

var james = {
    job: "programmer",
    married: false
};

// set to the first property name of "james"
var aProperty = "job"


// print the value of the first property of "james" 
// using the variable "aProperty"
console.log(james[aProperty])

#10

I just wanted to post an FYI for anyone else in my position. I had the same code above that criticalcritics posted but my answer wasn't being registered as correct. After staring at my screen for 20+ minutes, I closed the browser and restarted the tutorial. I hit Save & Submit code and it worked.


#11

good thing your code works, have a nice day


#12

Crikey! A simple question was asked and you pretty much just RTFM'd as an answer. This person has already been through the lessons up to this point and just wants a hand with this specific question. I don't think printing a full How Objects Work is particularly helpful.


#13

Agreed. "If you can't explain it simply, you don't understand it well enough!" ~ Albert Einstein


#14

Hey, on line 7, it should actually say var aProperty = job;
Also, when you say console.log(aProperty); it should be console.log(james[aProperty]);
I hope that helped!


#15

I have tried that as well, but it still doesn't work. Is this question glitched?


#16

solution

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = "job";

// print the value of the first property of "james" 
// using the variable "aProperty"

console.log(james[aProperty]);

#17

It´s a correct code

var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = james.job;

// print the value of the first property of "james"
// using the variable "aProperty"
james[aProperty];

console.log(aProperty);


#18

var aProperty = "job" that is what everybody is using here.
I need help...I want to use curly bracket notation... It doesn't work though...
var james = {
job: "programmer",
married: false
};

// set to the first property name of "james"
var aProperty = {
job: "programmer"
};

// print the value of the first property of "james"
// using the variable "aProperty"
console.log(james[aProperty]);


#19

OF course that code works when we write var aProperty = "job", but what about bracket and dot notations?


#20

Same here. Took me 10 minutes mess around with it and eventually see your comment. Closed the tab, reopened it and the code worked. Frustrating...