There are two ways to access object properties:
| objectName.propertyName |
| objectName[“propertyName”] |
Example
| person.lastName; |
| person[“lastName”]; |
Methods are actions on objects, defined as functions stored in property values.
|
Property |
Property Value |
|
firstName |
John |
|
lastName |
Doe |
|
age |
50 |
|
eyeColor |
blue |
|
fullName |
function() {return this.firstName + ” ” + this.lastName;} |
Example
| const person = { firstName: “John”, lastName : “Doe”, id : 5566, fullName : function() { return this.firstName + ” “ + this.lastName; } }; |
In the example, this refers to the person object, with this.firstName and this.lastName accessing the person’s firstName and lastName properties.