Prototypes in JavaScript - FunFunFunction #16
Skills:
Systems Design Basics80%
Key Takeaways
Explains prototypes in JavaScript for systems design
Full Transcript
I am MPJ and this is fun function. To you new viewers, I do not normally look like this. I just h I'm a bit hung over today, so I just feel like wrapping myself in a hoodie. So, what are prototypes? Let's say that you have food in your software and you um you want to make more kinds of food. You want to make uh waffles. I guess well, if you have waffles, you need no other food. But um let's say you maybe like carrots or something. In that context, food is the prototype of waffles and carrots. Some of you are now thinking, hm, that reminds me of classes. And you would be right because prototypes do remind of classes, but they are not the same thing. So for this video, try to put classes aside and see prototypes as a new thing and not shove prototypes into that part of your brain where you keep your classes because then you will just be confused and unhappy. Anyway, let's create a food object. const food equals uh object literal. We're going to give it an init method. And that init method is going to take a time. H and we're going to assign that type to a property on the current object. And we are going to have an all important eat [Music] method console.log log you ate the dot type. So as you see this is just a completely normal object. It has an init method uh that assigns the type. In fact we're going to do that now. Going to go food.init and we going to have a waffle. And it has the all important eat method that uh logs out you ate the uh this type. Let's let's use that now. Food. And let's run that. Prototypes.js. That's the file. And you ate the waffle. This is fine. But what if we want to create more types of food objects like those carrots? I just realized that I should give you full disclosure here because my teeth might be red. I'm drinking some red wine. Anyway, carrots, this is where object.create comes in. I'm going to delete these two lines here. And I am going to do a new con do waffle. And we're going to do object.reate food. And on this one, we're going to do net. So give it type waffle. And we're going to go waffle eat and run that. You ate the waffle. We're back to where we started. But let's get that carrot into place too. Using const here. Uh const means that you cannot change the variable later. Uh otherwise it's just like let. You should always use const if you don't explicitly need uh the mutability of um of uh of let or var for that matter. You should not use var ever unless you're in some old environment or something. Anyway, uh carrot dot in it. carrot and carrot dot eat. You all know what this will return. You ate the waffle. You ate the carrot. Looking at this example, you might infer that object. creates a copy of the object. Let's see if that is true. Going to move this up a bit. Um, and I'm going to do this [Music] food dot eat plus function console.log log you to eight the dot upper case or two uppercase [Music] right and we're on it what do you think will happen Uh, I will suck and not being able to code. Right. Try again. You totally at the apple and you totally at the carrot. But this is kind of strange. This means that object.create create cannot possibly be creating copies because if it did this food eat would not be affecting the the eat uh methods of waffles and carrots because they should be copies, right? But they do says you totally ate the waffle. The new method signature does affect the um the waffle and carrots even though we assign it to the food object. This is of course because the object.create does not create a copy of the food object. Instead, what it does is that it creates a new empty object for waffle and carrot and it assigns food to be a sort of fallback for those. So whenever we call a method or a property on on a carrot or a waffle uh that doesn't exist on the carrot and the waffle, it will look in its uh prototype object, the food object, and see if it's there. And then if it is, it will use that one. And that is what we mean by waffle and carrot having food as its prototype. So object.create create creates a new object and assigns food as its prototype. But one fallen carrot will only fall back to food if it lacks the property. If it has the property, it will use that. Let me show you what that means. I am going to delete all the carrot stuff from here. Carrot. Carrot. Carrot can delete. And we no longer need this example. Uh, but I am going to add another eat method. And I'm going to clear this. And now we're going to run it just to see what it looks. Um, prototypes.js. You ate a waffle. You ate a waffle. You ate a waffle. You ate. Now let's see what if I do this food dot type. What will he type? He will type. When I run, what do you think will happen? I will give you a golden star if you figure this out before I run it. What do you think? Three, two, one. You knew that one, right? But what if I comment out? They're in it. Let's check it. Node prototypes. Yes. Did you guess correctly? You guessed. You ate the undefined. You ate. So in this first eat call here, waffle will not have a uh anything assigned to the type property. And when it looks in its prototype, the food object, it will not have anything there either. So it's going to return undefined. However, here we assign uh this string to the uh the type property on the food object, which means that when in this second call here, waffle. Well, it won't still won't find any type property on itself because well, that's assigned here in the init function and the init function is commented out. But it will fall back to its food prototype and there it will find the food typeyp stuff and I'll put it here. I also quickly want to mention that you can use prototypes to do type checking. Going to delete this part here and bring back the carrot and uh waffles that we saw before. And now you can actually do food dot is prototype of waffle. We could do food is prototype of 1, two, three, four, five. Some integer and food is prototype of carrot. Some is food. Waffle is food. Wow, this is super exciting. [Music] entertainment and I we're going to do int is food. Do you know what this is going to be? I think you do, right? I think you're anticipating this. Are we running it? No. JS and waffle is food. True. intent is food false and just true. Yeah, we're going to do carrot is food. No. What? Okay. Oh, carrot is food. Clear. Clear. Oh my god, I'm so tired. Right. Carrot. Carrot. Carrot. car is food true. So you see that you can use the food prototype to see if that is used as a prototype uh in different uh different objects and that is an excellent way of doing type checking and that is the basics of prototypes. There really isn't much more to prototypes than that. However, while the concept of the prototype is simple, it does have some very cool implications for the language. So, on the next episode, we're going to explore that a bit. We're going to explore why prototypes are more powerful than classes, and I'm going to show you some cool tricks that you can do with it. Do not miss that episode. As usual, it will be released next Monday morning, 0800 GMT. But if you want more videos like this one right now, you should check out either the composition over inheritance video or the factory functions video. I am MPJ. This is fun function. Until next Monday, stay curious. [Music]
Original Description
💖 Support the show by becoming a Patreon
https://www.patreon.com/funfunfunction
Let's say that you have a food type, and you want to make lots of different instances of that food, such as waffles, and I don't know why you'd want any other food than waffles, but maybe you like carrots or something. In that context, *food* is the prototype of *waffles* and *carrots*.
Some of you will be thinking - that reminds me of classes! You'd be right, but they are not the same thing, so try to put classes aside and think of prototypes as something completely new - if you try to shove prototypes into the same place in your brain where you keep classes you'll be very unhappy.
Music by http://incompetech.com/
💛 Follow on Twitch
We record the show live Mondays 7 AM PT
https://twitch.tv/funfunfunction
💛 Fun Fun Forum
Private discussion forum with other viewers in between shows. https://www.funfunforum.com. Available to patron members, become one at https://www.patreon.com/funfunfunction
💛 mpj on Twitter
https://twitter.com/mpjme
💛 CircleCI (Show sponsor)
Robust and sleek Docker-based Continuous Integration as a service. I used CircleCI prior to them becoming a sponsor and I love that their free tier is powerful enough for small personal projects, even if they are private. Use this link when you sign up to let them know you came from here:
https://circleci.funfunfunction.com
💛 Quokka (Show sponsor)
Wonder how MPJ evaluates JavaScript inline his editor. Quokka is the answer - use this link when you buy to let them know you came from here:
http://quokka.funfunfunction.com
💛 FUN FUN FUNCTION
Since 2015, Fun Fun Function (FFF) is one of the longest running weekly YouTube shows on programming 🏅 thanks to its consistency and quality reaching 200,000+ developers.
🤦♂️ The Failing Together concept is what makes FFF unique. Most coding content out there focus on step-by-step tutorials. We think tutorials are too far removed from what everyday development is like. Instead, F
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 25 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
▶
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
58
59
60
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
Too many tools and frameworks!
Fun Fun Function
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
Staying relevant as a programmer
Fun Fun Function
Factory Functions in JavaScript
Fun Fun Function
Composition over Inheritance
Fun Fun Function
Software needs to be better - FunFunFunction #1
Fun Fun Function
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
Clojure - FunFunFunction #5
Fun Fun Function
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
Functors - FunFunFunction #10
Fun Fun Function
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
Questions and Answers - FunFunFunction #12
Fun Fun Function
Streams - FunFunFunction #13
Fun Fun Function
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
Coders are herd animals - FunFunFunction #18
Fun Fun Function
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
Monad - FunFunFunction #21
Fun Fun Function
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
Questions and Answers - FunFunFunction #24
Fun Fun Function
Losing motivation - FunFunFunction #25
Fun Fun Function
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
What editor do you use? - FunFunFunction #31
Fun Fun Function
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
Haskell Basics - FunFunFunction #35
Fun Fun Function
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
Haskell lists - FunFunFunction #39
Fun Fun Function
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function
More on: Systems Design Basics
View skill →Related Reads
📰
📰
📰
📰
Accueil web animé en 2026 : analyse de l’expérience Mini Ayoub de WeWorkWeb
Medium · UX Design
Custom Admin Dashboard Next.js Guide
Medium · Programming
The Stale Closure Problem in React.
Medium · JavaScript
Mastering Angular ControlValueAccessor (CVA): The Complete Guide with Real-World Examples
Dev.to · Abanoub Kerols
🎓
Tutor Explanation
DeepCamp AI