Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)

Net Ninja · Beginner ·🛠️ AI Tools & Apps ·8y ago
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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →