Creating Objects
Objectives
Create a constructor function
Use a constructor function to create an object
Explain what a constructor function is and how it works
Explain what
this
is in the context of an object
Introduction
So far we have mainly examined primitive data types like strings and integers. As you have seen, it sometimes becomes convenient to represent data with objects, which gives us key value pairs. For example, we may represent a user as the following:
Now imagine that we had a couple of users:
Great. Two nice users.
Constructor Function
Instead of typing out each attribute separately with the literal syntax, we can use a constructor function. It operates as a factory for new objects.
Let's write a constructor function that returns a Javascript object without any attributes:
Let's unpack the code above. First, we declare a function called User
. We capitalize the name of the function simply as a convention, to indicate that we will be using this function differently. This function is just a plain old JavaScript function. We demonstrate that by calling User()
and seeing that it returns undefined, just like any other blank JavaScript function would.
However, when we call this function with the new
keyword, things change. First, JavaScript creates a new object, {}
. Second, the newly created object is automatically returned from the function. Note that this occurs even though there is no explicit return inside of the function. Not exactly how we remember functions operating.
Now, as promised, we want our constructor function to provide a mechanism for standardizing the attributes we assign to the object. We can do this by defining our constructor function with some arguments.
So mission accomplished, sort of. We see that we can define our constructor function to take in three arguments, and name those arguments appropriately. However, when we execute the function we are not doing anything with the data that we are passing in, and therefore we are still returning the same blank object.
Before we can write the code to return our new object with specific attributes we need to know a couple other things about constructor functions. First, is that when we call a function with the new
keyword the body of the constructor function is run. Let's see that:
So the name and age are logged, just like our function instructs. But what we want to do instead of logging the information is write code to access the newly created object and assign the passed-in arguments as attributes. How do we access that newly created object? With the this
keyword.
We'll explore this
in more detail in a later lesson, but for now know that this
references the object that receives the method call. It allows us to access the object receiving the method from inside a method call. Here, when we call a function with the new
keyword, the object receiving the method call is the newly created object. And so when you see the new object logged twice, the first time is from the console.log(this)
code, and the second time is because the constructor function returns the newly created object. To reiterate, when we call our constructor function, this
references the object that received the method call, i.e., the newly created object.
Our final step is to modify that object by assigning it some attributes accordingly.
As before, when we call our constructor function it creates the new JavaScript object. We refer to that JavaScript object from inside of our function with the this
keyword. We then assign that new JavaScript object attributes with values that we receive from the arguments passed through.
It's an easy game from here
The objects that we return operate just like the JavaScript objects you have seen before. You can read from the object with either the dot or bracket syntax.
And we can modify the objects by assigning new values with dot or bracket syntax:
And of course we can create as many objects as we want with our constructor function.
Summary
We've reviewed working with objects in JavaScript, and started to think about object-oriented programming by applying the constructor function pattern. This allows us to easily define and reuse objects that we design.
Last updated