Objects
Objects are another way of handling data. In arrays the indexes are commonly numerical; objects give us a robust way of assigning and retrieving data. Objects are derived from the object-oriented programming concept; a programming paradigm that is very popular. Objects are a virtual representation of real-time data; they allow us to organize our data into logical groups via properties and methods. Properties describe the state of the object, while methods describe the behavior of the object. Properties are a key-value pair that holds the information. Take a look at the following:
In the previous example, we have instantiated a person
object, and then added the firstname
and lastname
properties that described the object. We added behavior to the object by creating a method called
getFullName
, the method accessed the object properties, retrieved the data, and alerted the output onto the screen. In this example the properties are accessed by the dot notation; we could also access a property by putting the property name in square brackets similar to an array, but it is not popular. This is shown as follows:
The second way of creating an object is by using the curly braces. Here we are introduced to the this
keyword, which provides a reference to the object's properties and methods, as shown in the following: