Object Oriented JavaScript Tutorial #4 - Classes
Skills:
JavaScript Fundamentals90%
Key Takeaways
Introduces classes in object-oriented JavaScript and their emulation using the class keyword
Full Transcript
so we've made this user object right now with methods and properties but what if we wanted more than just one user object well we've seen that what we could do is create several different user objects like this one for each different user but notice we're repeating ourselves a lot we've rewritten the login and logout functions several times these properties several times and quickly this could get out of hand what if instead I wanted an easier way to create a user object what if I wanted to do something like this I could say VAR user four is equal to a new user and then just pass in some parameters over here like so well we can do this and we can do this in a couple of different ways now one way is to use the parameter time model but with the release of es6 we gained a little syntactic sugar so that we can solve this problem using classes as well now under the hood classes essentially do the same thing as working with prototypes in JavaScript but some people think that classes are nicer or easier to work with and they do not need to as well however not everyone likes this new class syntax since JavaScript as a language doesn't really have classes built into it so this is just some syntactic sugar which emulates the idea of classes in JavaScript and under the hood classes are just doing the same thing as the prototype model so we'll focus on classes for now and try to keep it simple to begin with but I will also add on some lectures towards the end of this series about what's actually going on under the hood using prototypes because I think by understanding what's going on behind the scenes it's gonna make you a much better developer so anyway back to the problem at hand if we only ever need one type of object then something like this would do just making an object literal right but if we ever want to create multiple versions of the same type of object like this we want to create several different users then what we're going to do is use a class to do that instead of manually creating several different object literals like we have done right here because a JavaScript class allow to easily create multiple objects of the same type by doing something like this so you can think of a class in JavaScript as a bit like a blueprint that describes a particular object in a non specific way for example a class that describes a car would have a color property and every car would have that property it would have a color so we define those properties in our class for the car our blueprint for the car but we don't say what color the car is because that is specific to each individual instance of that class so when we create a new instance of this class we're creating a new car object and then at that point where we create it we pass to the class a parameter which is the color of that car we can say okay we want to use the car class to create a red car or a blue car or a green car or a purple car and so forth we can easily create these car objects using the car class and we pass in that color each time around so that all the same type of object there are cars but they all have different property values they all have a color but with a different value in the same way we could have a class for a user so a user has an email address a name maybe a status to say whether they're online or offline that could be true or false they also have a login method and a logout method now whatever we create a new user we're going to use this class and we're going to pass in the values of these different things right here so we could create user one and pass in these values and user 2 and pass in these values in both cases these objects that we're creating for each individual user is using this user class but they're different instances of that object they have different values they have the same property names email name status login and logout so they have all the same kind of mechanics under the hood but they have different values so what we're going to be doing is using a class to create instances of that type of object okay so let's keep it simple to begin with and just create an empty class so we'll get rid of all of this junk that we've written over the last couple of lessons and instead create one single class now the way we do this is by saying first of all the keyword class that says we want to create a class then the name of the class itself now Convention says that we start this with an uppercase letter so I'll call it user with a capital u then we open and close our curly braces and this right here this is now an empty user class so it's set up and ready to go and we can start adding properties and methods to this class in the next video
Original Description
Hey gang, in this Object Oriented JavaScript tutorial, we'll take a look at classes and why we'd use them. JavaScript classes can be used to easily create objects of a specific type/class. Although classes don't technically exist in JS, the class keyword emulates the idea of having classes.
🐱💻 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
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 53 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
▶
54
55
56
57
58
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