- 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
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 email@example.com stacylacey for the username and firstname.lastname@example.org 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 email@example.com. and email with the value of firstname.lastname@example.org.
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 email@example.com. and a email address of firstname.lastname@example.org. 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.
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.