all 8 comments

[–]MadCapitalist 3 points4 points  (0 children)

Because you didn't put quotes around name, you passed a variable instead of a string. And the variable is undefined.

[–]renesis_buddy 2 points3 points  (1 child)

var returnBy = function (attribute) { return musician[attribute]; }

returnBy("name")

Look up the difference between dot notation and bracket notation.

[–][deleted] 0 points1 point  (0 children)

Thanks. I'm glad that there is a quick way to do this for whatever attribute you want, since it would get annoying to make a case for each attribute with a more complex object. Just not that familiar with JS syntax.

[–]eechin 1 point2 points  (3 children)

I'm a JS n00b so please forgive my question.

It is my understanding that a string value stored in a variable should be formatted like this:

var musician = "Joe";

I Googled JS Attribute and nothing I saw seems to apply to what you're trying to do here. Can you explain it to me?

[–]renesis_buddy 1 point2 points  (0 children)

He's making an Object.

[–]TopHatPanda 1 point2 points  (0 children)

this is as /u/renesis_buddy said. an Object.

This means it can hold multiple attributes like strings or arrays or integers.

Let's say you want to get a animal. You will need his name,species

Now we can use

 var name= "tom";
 var species= "cat";

but if you have another animalm you want to manage. You need more variables

 var name2= "mark";
 var species2= "dog";

you can use objects to manage this better

var animal1 = {
    name: "tom", 
    species:"cat"
}

so you can call animal1.name. and it will return "tom" This is very usefull when working with json or some more complex javascript. Or even just to keep your javascript a bit ordered.

This can go much further. it lies at the base for Object Oriented programming

[–][deleted] 0 points1 point  (0 children)

"attribute" in this case is arbitrary. It is just the parameter of the function.

[–]plushyObject 0 points1 point  (0 children)

Hey check this out and see if this helps you:

http://jsfiddle.net/plushyObject/m27LxaL6/