Run the below snippet in chrome's developer tools.
function fullName(initial,firstName) {
return initial + "." + firstName;
}
var person = [{
firstName:"Sam",
initial:"M",
age:42,
address:{nbr:10, street:"Street1", place:"Oakland"},
name:fullName(person[0].firstName,person[0].initial)
},
{
firstName:"Shyam",
initial:"N",
age:42,
address:{nbr:10, street:"Street1", place:"Oakland"},
name:function(){return this.initial + "." + this.firstName;}
},
{
"first-name":"Ryan",
initial:"S"
}
];
console.log(person[0].name);
console.log(person[1].name());
console.log(person[1].name);
console.log(person[1].address.place);
console.log(person[2]["first-name"]);
If the object key has a hyphen in it, you won't be able to use it directly but will work if is surrounded with quotes.
As you can see from above object literal
person declared has key
first-name surrounded with quotes.
In order to refer to the property, you will use:
person[2]["first-name"];
Output:
M.Sam
N.Shyam
function (){return this.initial + "." + this.firstName;}
Oakland
Ryan