With JavaScript, we are working with objects that are based on prototypes.
Object properties define the specifics of this one particular object.
Each object is a unique instance of an object prototype.
Objects have features that allow us to change their properties.
Methods: Property-changing features inside objects.
Objects can contain other objects.
JavaScript objects are collections of data and functionality stored as properties and methods that describe the object and what it can do.
To define an object, to create it, first we need a variable to hold the object (usually a const).
Object Containers
The object needs somewhere to live and it needs a name. For this, we use a container called a variable.
Objects Are Typically Constants: We can change the properties of the object inside the container. We can’t remove or replace the object from the container.
Accessing Object Properties
In most cases, use dot notation because it’s easy to understand. If you need to pass a variable into the property name, or you need to access a property that is somehow breaking convention, use bracket notation.
While classes are the preferred tool for creating object templates, there is another shorter and less advanced way of doing the same thing, which relies on a basic function.
The object constructor function captures the properties of the new object using its parameters and then defines and assigns values for each property and method using the this keyword and dot notation.
You’ll notice that the difference between the class and the object constructor function here is the methods live inside the main construction function, just like the properties do.
The end result is exactly the same as with the class but there are some significant differences.
The class allows us to do more things.
We can extend classes.
We can add new features to them that are not available inside an object constructor function.
And the class is now the preferred tool for creating objects based on a blueprint.
Use a class unless you are required to use an object constructor function because the classes give you more capabilities than the object constructor function does, and the only reason to use the older function is if you are running it in an old code base or in old infrastructure that have yet to support classes.