Objects
const hero = {
name: 'Bruce Wayne',
alias: 'Batman',
catchphrase: 'To the Batcave!',
speak: function() {
return 'Attention! ' + this.catchphrase;
},
attack: function(sound) {
return `(punches bad guy) ${sound}`;
}
};
// Access a property
console.log(hero.name); // Bruce Wayne
console.log(hero['name']); // Bruce Wayne
// Access a property with bracket notation and property as variable
const thingToLookFor = 'alias';
console.log(hero[thingToLookFor]); // Batman
// Access a method (functions in a object)
console.log(hero.speak()); // Attention! To the Batcave!
console.log(hero.attack('POWWWWW')); // (punches bad guy) POWWWWW
console.log(hero.speak);