Understand Function in an Object

Function in an Object

Data and functions organized within an object allows us to hold pieces of related information.

Object Syntax

Functions have the same syntax when they are in an object except for how their name is declared. Instead of let variableName = objects use variableName: to declare variables. This is true for functions and key value pairs.

Using functions you write the object name followed by a period followed by the function name with parentheses and any arguments in those parentheses.

let aPersonObject = {
name: 'Andrea',
age: 39,
country: 'USA',
friendFunction: function(friend) {
return 'Andrea and ' + friend + ' are friends!';
}
}
console.log(aPersonObject.friendFunction('Alfred'));