Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Skills:
JavaScript Fundamentals90%
Hey ninjas, in this Object Oriented JavaScript tutorial, I'll explain how we created objects before the class keyword was introduced - using the original constructor functions.
🐱💻 Course Links:
- VS Code editor - https://code.visualstudio.com/
- GitHub repository (course files) - https://github.com/iamshaunjp/object-oriented-js
- JavaScript for Beginners - https://www.youtube.com/watch?v=qoSksQ4s_hg&list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET
🤑 Donate @ https://www.paypal.me/thenetninja
🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
👾 Video thumbnail artwork by PixelSwish @ https://www.youtube.com/channel/UCGKSD3mitWl5UpMxZzaIrRA
What You'll Learn
Explains constructor functions in object-oriented JavaScript and their use before the class keyword
Full Transcript
or rather than ganks so we've looked at classes so far to create these different objects but earlier in the course I mentioned that es6 classes were actually just a bit of syntactic sugar built on top of the JavaScript prototype model so JavaScript as a language doesn't really have classes the stuff we've been right in here as so far basically been pretend classes built into the language as a way to emulate how classes behave but classes are just a thin layer of abstraction built on top of the JavaScript prototype model now the JavaScript prototype model was the original way we tend to create or emulate classes in JavaScript before we had this class keyword and when we now create a class using the new es6 class keyword javascript is still working the same way as it did before to emulate these classes using the prototype model under the hood now you might be scratching your head thinking why am i telling you what this and why do I even need to know now that we just have this class keyword can I not just use that instead of worrying about the prototype model and what's going on under the hood and well yeah if you don't feel and need to understand the inner workings or want to be a more complete JavaScript developer that's fine you don't need to learn it but by learning the prototype model you'll have more flexibility when it comes to working with objects debugging code or even working with all the developers code who may have used the prototype model because you can't be sure that everyone wants to use classes right so that's why I want to teach you to you I do think it's important to learn so there you go now you know the classes are just syntactic sugar and before they were introduced we had to emulate classes a different way which is what I'm going to show you around now so let's just think for a minute when we create a class using this class keyword right here what is it that's actually responsible for making up that object when we want to create one based on that class well it's a combination of things really but mainly it's the constructor function or right here that is the thing that binds all of the properties and values to the object when we create it right here all right so even though we're not going to be using the class keyword now to emulate a class we'll still need some kind of constructor function to construct the objects okay so first of all let's get rid of all of this junk we don't need this anymore because we won't be using classes now I'm going to keep var user 1 and user 2 will still use those later on but the rest of this stuff I will delete ok then so we need to create this constructor function so it's just a normal function and we'll call it user with a capital u like so now before the class that we created was called a user with a capital u now it's the constructor function let's call user with a capital u because we're not using the class keyword anymore so this constructor function rakion now represents our user class it creates user objects now when we want to create a new user object we can still create that in the same way using this new keyword now here we don't need to change that one bit because remember when we use classes the class keyword JavaScript is still doing all of the same stuff behind the scenes so this new keyword right here this is still doing all the same kind of stuff it would be doing for the class under the hood alright so again what that new keyword is doing is creating first of all an empty object for us then it's binding the context of this equal to that object that into object and it's passing it into this constructor function or right here so then we have access to this as the empty object inside this constructor function so again what we could do is take those parameters inside this constructor function email and name and we're passing those in right here yep and then what we can do is we can add those properties to this keyword to the new object that we've passed into this constructor function so we can say this email is going to be equal to email the thing that we receive right here and also this dot name is going to be equal to name I'm also going to give this an online property list online which says whether the user is online or not and I set that equal to false to begin with we don't need to pass in as a parameter because it's always going to be set to false to begin with when we create a new user now remember in the last videos we also added methods to our class so let's just add a method to our constructor function right here we'll say this dot log it is going to be equal to a function like so and inside this function what we'll do is say console dot log and we'll say this dot email has logged it okay all right then so let's just add down here i console log to see if this works well log user 1 first of all to the console and then we'll say also user to login and we'll call that function on user 2 okay so now I'm going to save this and just head over to the browser to see if this works and we can see now that in the console first were logging this user right here which has these properties email name online and then we're saying Yoshi and Mario Kart which is user twos email has logged in so this is our working and I can call those from the console as well we can see user two is Yoshi and Mario Kart comm etc ok so this is all working now so if you think about it we're still kind of emulating a user class this way it's just that now we're no longer using the class keyword this is the kind of stuff we do before the class keyword under the hood right now then this is fine but generally when we want to attach methods to an emulated class like this we don't put those methods inside the constructor function instead we use the prototype property on this constructor function and we'll see exactly what that looks like in the next video
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 58 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
▶
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: JavaScript Fundamentals
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
The Future of Technical Education: AI, Projects, and Industry Collaboration
Dev.to AI
I Asked Gemini AI to Preview My Haircut Before My Salon Appointment - Here’s What Happened
Medium · AI
Top Five Free AI Tools in the Industry
Medium · ChatGPT
7 Best AI Tools for Research, Coding, and Development in 2026
Medium · Data Science
🎓
Tutor Explanation
DeepCamp AI