Latest / CyberSecurity Summary / You Don't Know JS: this & Object Prototypes
Transcript
- 0:00Speaker 1: Welcome to the deep dive. Today. We're going deep on JavaScript,
- 0:05Speaker 1: specifically your notes on classes. We've got an excerpt here
- 0:10Speaker 1: from You Don't Know Js This and Object Prototypes by
- 0:15Speaker 1: Kyle Simpson.
- 0:16Speaker 2: Oh yeah, I love that book.
- 0:17Speaker 1: And it looks like you're trying to figure out this
- 0:19Speaker 1: whole class thing, which even experienced developers sometimes get tripped
- 0:24Speaker 1: up on.
- 0:24Speaker 2: Yeah, for sure, it can be a bit of a
- 0:26Speaker 2: head scratcher.
- 0:27Speaker 1: So why don't we jump right in? The book dives
- 0:30Speaker 1: right in and says JavaScript doesn't actually have.
- 0:33Speaker 3: Classes right out of the gate.
- 0:35Speaker 1: So for someone writing JavaScript code, why does this matter?
- 0:41Speaker 2: Well? I think it matters because a lot of folks
- 0:44Speaker 2: come to JavaScript from other languages, okay, and they expect
- 0:47Speaker 2: it to work the same way like with traditional classes
- 0:50Speaker 2: and inheritance. But JavaScript has its own way of doing things.
- 0:54Speaker 2: So classes in JavaScript are more of a design pattern,
- 0:57Speaker 2: a way to structure your code to kind of mimic
- 1:00Speaker 2: that class behavior you see in other languages.
- 1:03Speaker 1: Got it. So you're saying we're basically creating an illusion
- 1:06Speaker 1: of classes exactly.
- 1:07Speaker 2: It's a bit of smoke and mirrors.
- 1:09Speaker 1: That's a really good way to put it.
- 1:10Speaker 2: To make it feel familiar.
- 1:11Speaker 1: I like that, So could you maybe give us an
- 1:14Speaker 1: analogy to help visualize this concept?
- 1:16Speaker 2: Sure, thing. Let's say you have a blueprint for a house.
- 1:20Speaker 1: Okay.
- 1:21Speaker 2: This blueprint represents the class, It defines the structure, the
- 1:25Speaker 2: room's the layout, you know. Yeah, but you can't actually
- 1:29Speaker 2: live in the blueprint itself, right.
- 1:31Speaker 1: You can't open a door on a blueprint exactly.
- 1:34Speaker 2: You need to build an actual house based on that blueprint.
- 1:37Speaker 1: Yeah, okay, I see that. That makes a lot of sense, right,
- 1:39Speaker 1: The blueprint is the plan, but the tangible house, yes,
- 1:42Speaker 1: that's the object we interact with exactly. So how does
- 1:46Speaker 1: JavaScript handle inheritance, right? If there aren't any real classes, Well, that's.
- 1:50Speaker 2: Where the magic of prototable inheritance comes in.
- 1:52Speaker 1: Okay.
- 1:53Speaker 2: Instead of copying behavior from parent to child classes like
- 1:56Speaker 2: in traditional inheritance, okay, JavaScript uses a system of linking
- 2:01Speaker 2: objects together. Each object has this internal link called prototype Okay,
- 2:07Speaker 2: The points to another object.
- 2:09Speaker 1: So it's kind of like a family tree, yeah, where
- 2:11Speaker 1: each person is linked to their.
- 2:12Speaker 2: Ancestors exactly, like a miniage.
- 2:14Speaker 1: An object can then access properties and methods from its
- 2:19Speaker 1: ancestral objects right through this link.
- 2:22Speaker 2: Yeah, it's like a chain of linked objects. Oh wow,
- 2:25Speaker 2: and this is what we call delegation. Okay, if an
- 2:27Speaker 2: object doesn't have a particular property or method, it delegates
- 2:31Speaker 2: the task up the chain to its prototype, okay, and
- 2:35Speaker 2: so on until it finds what it needs.
- 2:37Speaker 1: So it's like a chain of command.
- 2:39Speaker 2: But for code, that's a great way to think about it.
- 2:41Speaker 1: I like that. Yeah, so this delegation concept really changes
- 2:44Speaker 1: how I think about inheritings in JavaScript.
- 2:47Speaker 2: Yeah, it's a different way of thinking about it.
- 2:49Speaker 1: But if it's all about delegation, Yeah, why do we
- 2:52Speaker 1: still see terms like constructor and prototype?
- 2:55Speaker 3: Hmm, that's a good question in JavaScript.
- 2:58Speaker 2: Well, I think it's part of javascripts at tem to
- 3:00Speaker 2: make things you're more familiar to developers coming from those
- 3:04Speaker 2: class based languages. So you have the constructor function and
- 3:08Speaker 2: the prototype property. Okay, they're kind of part of this facade.
- 3:11Speaker 1: So they're trying to simulate the class like behavior but
- 3:15Speaker 1: using prototypes behind the scenes.
- 3:17Speaker 2: Exactly. They're like the stage props in this illusion of classes.
- 3:21Speaker 1: So JavaScript is really putting on a show here creating
- 3:24Speaker 1: this illusion of classes. It is, But the excerpt we
- 3:26Speaker 1: have here argues that understanding the mechanism behind this illusion
- 3:31Speaker 1: can actually help you write better code.
- 3:33Speaker 2: Absolutely, because once you see how the trick works, you
- 3:37Speaker 2: can use it more effectively.
- 3:38Speaker 1: Okay, I see what you mean there. Knowing how the
- 3:41Speaker 1: trick is done can help you use it better exactly.
- 3:43Speaker 1: So could you give us an example of, sure how
- 3:45Speaker 1: this understanding can lead to cleaner, more efficient code.
- 3:49Speaker 2: Sure. One way is through this style of coding called
- 3:55Speaker 2: OLO olo. Yeah, objects linked to other objects, which Simpson
- 4:00Speaker 2: really advocates for in this book.
- 4:01Speaker 1: Now sounds promising. Could you elaborate a little bit on
- 4:04Speaker 1: how it actually simplifies things absolutely compared to that traditional
- 4:08Speaker 1: class like approach in JavaScript.
- 4:10Speaker 2: Yeah, So let's compare them side by side. Imagine we're
- 4:13Speaker 2: creating a simple object representing a car, okay, a car object.
- 4:18Speaker 2: In the traditional approach, you might define a class using
- 4:23Speaker 2: a constructor function, and then you'd create instances of that
- 4:26Speaker 2: class using the new keyword.
- 4:28Speaker 1: Right, So that's the traditional way. Now with O low,
- 4:31Speaker 1: how would that same scenario look.
- 4:34Speaker 2: Well, instead of defining a constructor okay, you would create
- 4:37Speaker 2: a plane object okay that serves as the template for
- 4:41Speaker 2: your car, and then you can use object dot create,
- 4:44Speaker 2: object dot create to create new car objects that inherit
- 4:48Speaker 2: properties and methods from that template object.
- 4:51Speaker 1: Interesting, So we're kind of side stepping that whole class
- 4:55Speaker 1: illusion altogether, and we're working directly with the way that
- 4:58Speaker 1: JavaScript handles objects exactly.
- 5:00Speaker 2: We're embracing the prototypal nature of the language head on.
- 5:03Speaker 2: I like that.
- 5:04Speaker 1: So could we see an actual code snippet from the
- 5:07Speaker 1: book sure that demonstrates this?
- 5:09Speaker 2: Yeah? Absolutely, Yeah. The book excerpt has several examples. Let's
- 5:13Speaker 2: take a look at one. Great. In the traditional approach,
- 5:15Speaker 2: you might define a car class like this, okay, function
- 5:19Speaker 2: car make model, this make equals make this okay, model
- 5:23Speaker 2: equals model.
- 5:24Speaker 1: So we're setting up the constructor function there and adding
- 5:28Speaker 1: the start.
- 5:28Speaker 3: Method to its prototype exactly.
- 5:30Speaker 1: Okay, Now, how would we achieve that same functionality using OLO?
- 5:34Speaker 2: All right? So here's the OLO equivalent. Okay, constant car
- 5:37Speaker 2: prototype equals and curly brace okay start colon function parentheses,
- 5:41Speaker 2: curly brace okay, console dot log open parentheses quote the
- 5:47Speaker 2: plus this dot make plus space plus this model plus
- 5:50Speaker 2: is starting period close quote close, curly brace comma right
- 5:55Speaker 2: close curly brace semicolon got it, constant my car equal
- 5:58Speaker 2: object all right, create open meenthesis car prototype clothes parenthesis
- 6:02Speaker 2: semicolon my car okay, make equals quote Toyota close quotes
- 6:06Speaker 2: semi coolon my car dot model equals quote Camri close
- 6:11Speaker 2: quote semi colon okay. And then my car dot start
- 6:14Speaker 2: opening close parentheses semicolon wow.
- 6:17Speaker 1: Okay. So the difference is really striking.
- 6:19Speaker 2: It is, isn't it.
- 6:20Speaker 1: The OLO version feels so much more straightforward.
- 6:23Speaker 2: Yeah, it's much cleaner.
- 6:24Speaker 1: We're creating an object called car prototype that holds our
- 6:28Speaker 1: methods right, and then using object dot create to link
- 6:32Speaker 1: new car objects to this prototype. No more new keyword,
- 6:37Speaker 1: no more dot prototype references.
- 6:39Speaker 2: It's all about that direct connection between objects.
- 6:41Speaker 1: I like that so much cleaner, and it really reflects
- 6:44Speaker 1: the essence of JavaScript's delegation mechanism. You're explicitly defining the
- 6:49Speaker 1: relationship between objects without all that extra baggage of the
- 6:52Speaker 1: class syntax.
- 6:53Speaker 2: It's about cutting through the noise and getting to the
- 6:56Speaker 2: heart of how JavaScript works.
- 6:57Speaker 1: This is making me rethink all my old JavaScript.
- 7:00Speaker 2: I know, right, it's a real eye opener.
- 7:02Speaker 1: But I'm curious about ES six classes.
- 7:05Speaker 2: ES six classes.
- 7:06Speaker 1: They seem to offer a cleaner syntax as.
- 7:07Speaker 3: Well, don't they They do.
- 7:08Speaker 2: They definitely look cleaner on the.
- 7:09Speaker 1: Surface, So are they the answer then.
- 7:12Speaker 2: Well, not so fast. It's important to remember that ES
- 7:15Speaker 2: six classes or essentially syntactic sugar. They might look cleaner,
- 7:20Speaker 2: but under the hood, they're still based on the same prototype.
- 7:23Speaker 1: System, so it's not like they're a fundamental solution. It's
- 7:27Speaker 1: more like a cosmetic change.
- 7:29Speaker 2: Yeah, like a fresh code of pain on the same house.
- 7:31Speaker 1: So what are the implications of that for someone choosing
- 7:34Speaker 1: between ES six classes and OLO.
- 7:37Speaker 2: Well, that's a great question, and I think the answer
- 7:39Speaker 2: really depends on your priorities, Okay, and the specific project
- 7:43Speaker 2: you're working on. Some developers really appreciate the familiar structure,
- 7:47Speaker 2: sure and syntax that ES six classes provide. It can
- 7:52Speaker 2: make the code appear more organized and potentially easier to understand.
- 7:56Speaker 1: Right, especially for those coming from class based languages.
- 7:59Speaker 2: Exactly, I feel more comfortable with that syntax.
- 8:01Speaker 1: So in some cases, the familiarity factor might outweigh the
- 8:06Speaker 1: benefits of a more purist approach like OLO.
- 8:09Speaker 2: It could especially in larger teams or projects where maintaining
- 8:13Speaker 2: consistency and readability are.
- 8:14Speaker 1: Crucial, right, having everyone on the same page, exactly, But
- 8:18Speaker 1: there must be some downsides to using ES six class
- 8:21Speaker 1: classes if they're just masking the underlying system.
- 8:23Speaker 2: Right, there are definitely some trade offs.
- 8:25Speaker 1: Does it ever lead to confusion or.
- 8:28Speaker 2: It can, particularly if developers don't understand that ESIX classes
- 8:33Speaker 2: aren't true classes like in other languages, Right.
- 8:36Speaker 1: They're not really classes in the traditional sense.
- 8:38Speaker 2: They might fall into the trap of thinking in terms
- 8:41Speaker 2: of traditional class inheritance, okay, and miss out on the
- 8:44Speaker 2: flexibility and power right the JavaScript's prototype inheritance offers.
- 8:49Speaker 1: It's like learning a new language. You might be able
- 8:52Speaker 1: to get by with basic phrases, but without understanding the grammar,
- 8:56Speaker 1: you'll hit a wall when you try to express more
- 8:58Speaker 1: complex ideas exactly.
- 9:01Speaker 2: And that's the risk with relying solely on the EES
- 9:04Speaker 2: six class syntax okay, without grasping those core concepts of
- 9:09Speaker 2: prototypes and delegation, So.
- 9:11Speaker 1: It can actually limit your understanding. It can and potentially
- 9:14Speaker 1: lead to less efficient or even incorrect code.
- 9:18Speaker 2: Yeah, you might end up writing code that works but
- 9:21Speaker 2: isn't really taking advantage of JavaScript strengths.
- 9:23Speaker 1: So while yes six classes might seem appealing at first glance, yeah,
- 9:28Speaker 1: they don't replace the need to understand how a JavaScript
- 9:31Speaker 1: actually works.
- 9:32Speaker 2: Absolutely.
- 9:32Speaker 1: What would you say are the main advantages then of
- 9:35Speaker 1: embracing OLO, okay, especially for someone who's taken the time
- 9:38Speaker 1: to understand prototypes and delegation well.
- 9:42Speaker 2: For one, OLO aligns perfectly with the natural flow of JavaScript, okay.
- 9:46Speaker 2: It encourages you to think in terms of objects and
- 9:49Speaker 2: relationships rather than forcing this concept of classes onto the language.
- 9:55Speaker 1: Right.
- 9:55Speaker 2: This can lead to more efficient and expressive code.
- 9:58Speaker 1: It sounds like OLO allows you to work with the
- 10:00Speaker 1: grain of JavaScript rather than against it.
- 10:03Speaker 2: Exactly. You're working with the language of.
- 10:04Speaker 1: Strengths, and that aligns with the core principle of delegation
- 10:08Speaker 1: we've been discussing, right. It's all about objects delegating tasks
- 10:11Speaker 1: to one another rather than relying on this hierarchical class structure.
- 10:16Speaker 2: And that flexibility is one of JavaScript's superpowers, and OLO
- 10:20Speaker 2: lets you harness that power.
- 10:21Speaker 1: So it feels like OLO is not just a different
- 10:24Speaker 1: way to write code.
- 10:25Speaker 2: It's more than that, but a.
- 10:26Speaker 1: Different way to think about how objects interact in JavaScript.
- 10:30Speaker 2: It shifts your perspective from a rigid, class based mindset
- 10:36Speaker 2: to a more dynamic and interconnected view of objects.
- 10:41Speaker 1: I like that, and that shift in perspective can be
- 10:43Speaker 1: incredibly empowering for a JavaScript developer, it.
- 10:46Speaker 2: Opens up a whole new world of possibilities.
- 10:48Speaker 1: It's like we've been given this whole new set of
- 10:50Speaker 1: lenses to view the world of JavaScript objects.
- 10:53Speaker 2: Right, and with this newfound clarity.
- 10:55Speaker 1: And with this new found clarity, we can write code
- 10:58Speaker 1: that's more aligned with JavaScript's true exactly.
- 11:00Speaker 2: It's about embracing JavaScript for what it is, a powerful
- 11:04Speaker 2: and flexible language built on prototypes and delegation, rather than
- 11:09Speaker 2: trying to force it into this mold that doesn't quite fit.
- 11:11Speaker 1: As we wrap up this deep dive into JavaScript's classes,
- 11:14Speaker 1: what's the key takeaway you would like to leave our
- 11:17Speaker 1: listener with.
- 11:18Speaker 2: Hmmm, I would say experiment with both approaches. Yes, six
- 11:22Speaker 2: classes in olo. See how each one feels in practice,
- 11:26Speaker 2: how they impact your code structure and readability. The best
- 11:30Speaker 2: choice often depends on the context, your project and your
- 11:34Speaker 2: team's preferences. Makes sense, But the most important thing is
- 11:37Speaker 2: to go beyond the syntax Yeah, and truly understand the
- 11:40Speaker 2: underlying mechanics of JavaScript.
- 11:42Speaker 1: It's about making informed choices, not just blindly following the
- 11:46Speaker 1: latest trends or syntax.
- 11:47Speaker 2: Yeah, understanding the why behind the what.
- 11:50Speaker 1: Knowledge is power, especially in this ever evolving world. Of JavaScript.
- 11:54Speaker 2: It really is. And remember whether you choose to embrace
- 11:58Speaker 2: the elegance of OLO or leverage the familiarity of ES
- 12:02Speaker 2: six classes. Okay, the true beauty of JavaScript lies in
- 12:05Speaker 2: its dynamic nature and its ability to adapt to a
- 12:09Speaker 2: wide range of programming challenges.
- 12:11Speaker 1: So there you have it. We've demystified this whole concept
- 12:14Speaker 1: of classes in javascripts. He did explored the power of
- 12:17Speaker 1: prototypes and delegation, and we've discovered how OLO can offer
- 12:22Speaker 1: this more natural way to structure your code.
- 12:24Speaker 2: It could be a real game changer.
- 12:26Speaker 1: We hope this deep dive has given you the knowledge
- 12:29Speaker 1: and the insights to really navigate the world of JavaScript
- 12:32Speaker 1: objects with confidence.
- 12:34Speaker 2: I think it has.
- 12:35Speaker 1: And always remember the journey of learning and never truly
- 12:39Speaker 1: ends you ever stop exploring, So keep experiment and keep exploring,
- 12:43Speaker 1: and keep pushing the boundaries of what you can create
- 12:46Speaker 1: with this fascinating language.
- 12:49Speaker 2: Absolutely, JavaScript is full of surprises.
- 12:51Speaker 1: Yeah, it really is like a whole new perspective.
- 12:53Speaker 2: Yeah, it's a different way of thinking about things.
- 12:55Speaker 1: On how we structure our code for sure, and how
- 12:58Speaker 1: objects work in.
- 12:59Speaker 3: Javascripts can be really powerful, it can.
- 13:02Speaker 1: So as we wrap up this deep dive into JavaScript classes.
- 13:06Speaker 1: Any final thoughts, I.
- 13:09Speaker 2: Guess I would just encourage everyone to keep learning. Yeah,
- 13:12Speaker 2: you know, JavaScript is a language that's constantly evolving. It is,
- 13:15Speaker 2: there's always something new to discover, so stay curious, keep experimenting,
- 13:20Speaker 2: and never be afraid to dive deep.
- 13:23Speaker 1: That's great advice. So there you have it. Yeah, we've
- 13:26Speaker 1: debunked this myth of classes in JavaScript?
- 13:29Speaker 2: Who did?
- 13:30Speaker 1: Explored this whole fascinating world of prototypes and delegation, and
- 13:35Speaker 1: we've seen how OLO can really offer a more natural
- 13:38Speaker 1: and efficient way to structure your code.
- 13:41Speaker 2: It's a great tool to have in your JavaScript toolbox.
- 13:43Speaker 1: And hopefully now you feel even more equipped to tackle
- 13:46Speaker 1: any JavaScript challenge that comes your way.
- 13:49Speaker 2: You got this.
- 13:49Speaker 1: Thanks for joining us on this deep dive into JavaScript.
- 13:52Speaker 1: My pleasure, and we'll catch you next time.