CS133JS Beginning Programming: JavaScript

Object Constructors and Complex Objects

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7. Objects and Object Constructors
3. Conditional Statements: if and switch8. Web Page I/O
4. Repetition: while, do while, and for9. Review
5. Midterm Check-Point10. Final

Table of Contents

Introduction

Q and A

 

Review of Objects

Creating an Object Literal

Accessing an Object Property

The this keyword

Inside a method, the this keyword references the object that contains the method so you can access an object property.

Calling a Method on an Object

 

Object Constructors

Sometimes we will want to make multiple objects that are nearly the same. For example, we might want to make a "template" for creating a flock of pigeons. Here is one pigeon object:

What if we want more pigeon objects, but with different property values? Is there a way we can make a template that we can use to make others? Yes! We can make an object constructor.

Here's a special function that's an object constructor.

Notice that the first letter is capitalized (style, not syntax).

Now we can use the constructor function to make some Pigeon objects. We call the constructor with the keword new which indicates that we are making a new object. We set the property values in the new object by passing them in the constructor function call:

We can use these objects just like we would a literal object:

The this Keyword

Now that we have multiple objects that use the same object definition, we can see the importance of using this inside of object methods. The this keyword represents whatever object it is in and now it can be in multiple objects made by the same object constructor function.

Q: What would have happened if we used the object name inside the method instead of this?

 

Complex Objects

Object properties can be other objects. When we put objects inside of objects we say they are a complex object. That doesn't mean they are complicated, just that objects are composed with one or more other objects inside of them.

An Object Inside an Object

An object can have a property that has an object as it's value. For example we can make a loft object for our pigeons to live in (a pigeon house is called a loft). Here is a loft for just one pigeon. (The pigeon property is an object literal.)

Or, we can use an object constructor:

We access the pigeon in the loft like this:

 

An Array of Objects Inside an Object

We can also put objects in arrays and put arrays of objects inside objects.

Let's start with just an array of objects. Here is an array that represents a flock of pigeons. I could have written object literals in each element of the array, but it was much easier to use the Pigeon object constructor.

We can access an individual pigeon by it's array index:

Now let's create a loft object that has an array of pigeon objects:

Alternatively, we could just put a declaration for an array that can hold pigeon objects in the loft object:

And then add some pigeons:

We can access a pigeon in the loft like this:

What code could we write to make all the pigeons fly?

 

Examples

These are the same examples that are posted on Moodle.

GPA Calculator with Objects

This is a new version of the GPA calculator. (The old one used arrays.)

Movies and Actors with Objects

This is a new version of the Movie and Actors web app.

 

Alternate Syntaxes for Object Constructors

You will also see object constructor functions that internally create an object, set it's values and return it; rather than setting properties on this. Here's an example:

Or you might see an object constructor written like this, where the creation of an object is combined with assignment of properties:

And this can be simplified to:

 

References

Tutorials

Working With Objects - MDN Guide

JavaScript Object Constructors - W3Schools

 


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird, 2020, revised , are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.