Arrays within Object
by Nithi Nadar
JavaScript
var myProfile =
{
Name: 'Nithi Nadar',
Age: 44,
Height: '5.6',
Citizenship: 'USA',
Trophies: ['Gold', 'Silver', 'Bronze'],
speak: function(spokeWhat){
console.log(this.Name + ' says ' + spokeWhat);},
viewTrophy: function(i) { return this.Trophies[i];},
listTrophies: function() {
var listWithCSV = "";
for (var i = 0; i < this.Trophies.length; i++)
{
listWithCSV += this.Trophies[i] + (i < this.Trophies.length-1 ? "," : "");
}
return listWithCSV;
}
};
myProfile.speak("Hello");
console.log(myProfile.viewTrophy(2));
console.log(myProfile.listTrophies());
// Protect Trophies
var me = (function()
{
var Trophies = ['Gold', 'Silver', 'Bronze'];
// returns a new object
return {
Name: 'Nithi Nadar',
Age: 44,
Height: '5.6',
Citizenship: 'USA',
speak: function(spokeWhat){
console.log(this.Name + ' says ' + spokeWhat);},
viewTrophy: function(i) { return Trophies[i];},
listTrophies: function() {
var listWithCSV = "";
for (var i = 0; i < Trophies.length; i++)
{
listWithCSV += Trophies[i] + (i < Trophies.length-1 ? "," : "");
}
return listWithCSV;
}
};
}());
me.speak("Hellooo");
console.log(me.viewTrophy(0));