Prototypes in JavaScript - FunFunFunction #16

Fun Fun Function · Beginner ·🖌️ UI/UX Design ·10y ago

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 Higher-order functions - Part 1 of Functional Programming in JavaScript
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
2 Map - Part 2 of Functional Programming in JavaScript
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
3 Reduce basics - Part 3 of Functional Programming in JavaScript
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
4 Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
5 Reduce Advanced - Part 4 of Functional Programming in JavaScript
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
6 Closures - Part 5 of Functional Programming in JavaScript
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
7 Too many tools and frameworks!
Too many tools and frameworks!
Fun Fun Function
8 Currying - Part 6 of Functional Programming in JavaScript
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
9 Recursion - Part 7 of Functional Programming in JavaScript
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
10 Promises - Part 8 of Functional Programming in JavaScript
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
11 Staying relevant as a programmer
Staying relevant as a programmer
Fun Fun Function
12 Factory Functions in JavaScript
Factory Functions in JavaScript
Fun Fun Function
13 Composition over Inheritance
Composition over Inheritance
Fun Fun Function
14 Software needs to be better - FunFunFunction #1
Software needs to be better - FunFunFunction #1
Fun Fun Function
15 Unit testing: How to get your team started - FunFunFunction #2
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
16 Straight-line code over functions - FunFunFunction #3
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
17 Clojure - FunFunFunction #5
Clojure - FunFunFunction #5
Fun Fun Function
18 The growth stages of a programmer - FunFunFunction #6
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
19 5 tips to quickly understand a new code base - FunFunFunction #7
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
20 Semicolons cannot save you! - FunFunFunction #9
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
21 Functors - FunFunFunction #10
Functors - FunFunFunction #10
Fun Fun Function
22 Functors: I was WRONG! - FunFunFunction #11
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
23 Questions and Answers - FunFunFunction #12
Questions and Answers - FunFunFunction #12
Fun Fun Function
24 Streams - FunFunFunction #13
Streams - FunFunFunction #13
Fun Fun Function
Prototypes in JavaScript - FunFunFunction #16
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
26 Fast or Flexible? - FunFunFunction #17
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
27 Coders are herd animals - FunFunFunction #18
Coders are herd animals - FunFunFunction #18
Fun Fun Function
28 Weekend Kubernetes Shenanigans - FunFunFunction #19
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
29 Monad - FunFunFunction #21
Monad - FunFunFunction #21
Fun Fun Function
30 Moar Weekend Shenanigans - FunFunFunction #23
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
31 Questions and Answers - FunFunFunction #24
Questions and Answers - FunFunFunction #24
Fun Fun Function
32 Losing motivation - FunFunFunction #25
Losing motivation - FunFunFunction #25
Fun Fun Function
33 LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
34 Fast code is NOT important - FunFunFunction #27
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
35 Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
36 Writing unit tests for personal projects? - FunFunFunction #29
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
37 Let's Code a Pomodoro Button - FunFunFunction #30
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
38 What editor do you use? - FunFunFunction #31
What editor do you use? - FunFunFunction #31
Fun Fun Function
39 Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
40 Is Programming Art? - MPJ's Musings - FunFunFunction #33
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
41 Generators in JavaScript - What, Why and How - FunFunFunction #34
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
42 Haskell Basics - FunFunFunction #35
Haskell Basics - FunFunFunction #35
Fun Fun Function
43 Haskell - Baby's first functions - FunFunFunction #36
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
44 Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
45 How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
46 Haskell lists - FunFunFunction #39
Haskell lists - FunFunFunction #39
Fun Fun Function
47 var, let and const - What, why and how - ES6 JavaScript Features
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
48 Why are some programming languages popular? - MPJ's Musings  - FunFunFunction #41
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
49 Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
50 bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
51 Examples of this and bind - Object Creation in JavaScript P2 -  FunFunFunction #44
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
52 Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
53 Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
54 Cellular Automata - Pair Programming - FunFunFunction #49
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
55 The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
56 __proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
57 Unity game pair programming - Let's code - FunFunFunction #53
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
58 Throw out your tools - MPJ's Musings - FunFunFunction #54
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
59 Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
60 Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function

Related Reads

Up next
ASUS ProArt Live with Raj | How to EDIT Instagram REELS in Premiere Pro #LearnFromThePros
Raj Photo Editing and Much More
Watch →