The method we've used to create objects uses object literal notation—that is, creating a new object with { }
and defining properties within the brackets.
Another way of creating objects without using the curly brackets { }
is to use the keyword new
. This is known as creating an object using a constructor.
The new
keyword creates an empty object when followed by Object()
. The general syntax is:
varbjectName =newObject();
We then have to fill this object with properties and labels. How do we do that? Check out the creation of the object bob
to see what we do. We create the name
property for the object bob
by using bob.name
and assigning that to a value. Contrast this to how we define properties in lines 6-7 for the susan1
object.