Who's in your bracket


#1

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

i sincerely dont understand why it works..
var aProperty is set to a string "job"


#2

When you call james[aProperty] you are actually calling james["job"], because job is the string that is assigned to aProperty.

This would be useful in a function where you want to inspect the object james dynamically.

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

var inspectJames = function(phrase) {
  return james[phrase];
}

console.log(inspectJames("job"));
/*returns "programmer" since that is the value applied to job*/

console.log(inspectJames("married"));
/*returns false since that is the value applied to married*/

Hopefully this helps, this was the best way I could think of to explain it.


#3

@spiderpiggie
Your 'inspectJames' is somewhat confusing, as the exercise uses 'aProperty'. While it's always good to be able to alter your code with higher levels of precision, it drives newer coders to believe that the excersise is too advanced, and copy your code. Below, I have the exercise as I completed it. Your work was good, but I think that this looks a little more like what the exercise asks you to do.

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


#4

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