1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to JavaScript Object Constructors

Introduction to JavaScript Object Constructors

Contents

keyboard_tab
JavaScript Object Constructors
Introduction to JavaScript Object Constructors
Overview
Difficulty
Intermediate
Duration
3m
Students
12
Ratings
5/5
starstarstarstarstar
Description

This practical course explores JavaScript Object Constructors, a function that creates an instance of an object.

Learning Objectives

  • Learn how to create a constructor
  • Learn how to use the constructor to create multiple instances of an object
  • Understand how constructors bind properties and values to each object instance

Intended Audience

This course is intended for anyone who wants to learn about JavaScript Object Constructor.

Prerequisites

Anyone with an interest in using the rest operator in JavaScript or who wants to improve their knowledge of JavaScript in general.

Transcript

Introduction to JavaScript Object Constructors. On the screen, I have a user object. If I wanted another object with the same properties, I would have to write out another object literal to do so. As a developer, I want to be able to create multiple instances of an object that maintains the same properties for each instance of that object. You can think of an object constructor as an object blueprint. I will begin by commenting out the user object and constructors are created with functions.

So, I will type function User, and User is capitalize. So, I will type function User, and User is capitalize. And now, two parameters for each object property that I wish to use. In this case, userName and email. Now, inside of the function, I'm gonna type this.userName = userName. I'm gonna type this.userName = userName. Next, I will type this.email = email. Next, I will type this.email = email.

Now, inside of this function, I'm not writing out an object, I am writing out declarative statements that will be used to create an instance of an object. This completes the user object constructor. So now, I will create a single instance of the user object, So now, I will create a single instance of the user object, const userOne = new User, don't forget the capital letter const userOne = new User, don't forget the capital letter, and this is a function so I need to pass in my arguments, stacylacey for the username and slacey@example.com stacylacey for the username and slacey@example.com for the email address. And now, I'm gonna console.log . I will execute the code. And in the console, there is an instance of the user object with the email and username properties.

So, how does the constructor function work? The new keyword is used to create a new instance of the user object, finding the keyword this to this instance of the object, creating the property of username with the value of stacylacey and email with the value of slacey@example.com. and email with the value of slacey@example.com.

So where does reusability come into play? I'm gonna make another user, const userTwo = new User, I'm gonna make another user, const userTwo = new User, passing in the username of tracylacey and a email address of tlacey@example.com. and a email address of tlacey@example.com. I will now console.log and execute the console logs. And now, in the console, there are two instances of the user object. Both of them with their own set of properties and corresponding values. And that's it. Thanks for watching at Cloud Academy.

About the Author
Avatar
Farish Kashefinejad
Full-Stack Development Content Creator
Students
2182
Labs
19
Courses
45
Learning Paths
4

Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.

Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.

Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.

Farish is an avid powerlifter, sushi lover, and occasional Funko collector.

Covered Topics