Latest / CyberSecurity Summary / Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript (Effective Software Development Series)
Transcript
- 0:00Speaker 1: If I asked you to add point one and a
- 0:02Speaker 1: point two, you would probably just confidently say point three, right.
- 0:05Speaker 2: I mean, it's basic maths.
- 0:06Speaker 1: Exactly, it's basic math. But if you ask JavaScript to
- 0:09Speaker 1: do that exact same calculation, it spits out zero point
- 0:13Speaker 1: three zero zero zero zero zero zero zero zero zero
- 0:17Speaker 1: zero zero.
- 0:17Speaker 2: Yeah, which is usually the moment a new developer just
- 0:20Speaker 2: stares at their screen in total disbelief.
- 0:21Speaker 1: Oh absolutely, So, today we are taking a deep dive
- 0:24Speaker 1: into why one of the world's most you know, widely
- 0:27Speaker 1: used programming language fundamentally struggles with basic math, and more importantly,
- 0:32Speaker 1: how understanding its hidden quarks can actually make you a master.
- 0:36Speaker 2: Coder because it is a language built on honestly brilliant flexibility.
- 0:40Speaker 2: But that flexibility comes with this massive shadow, like if
- 0:43Speaker 2: you don't know the mechanical rules operating under the hood.
- 0:46Speaker 2: That shadow is exactly where all your worst bugs are
- 0:48Speaker 2: going to hide.
- 0:49Speaker 1: And that is exactly what we are waiting into today.
- 0:51Speaker 1: Welcome to the deep dive. Our mission today is to
- 0:54Speaker 1: move beyond just like memorizing JavaScript syntax, we want to
- 0:57Speaker 1: master its pragmatics. We're using a phenomenal source text today,
- 1:01Speaker 1: Effective JavaScript by David Herman, and the whole goal here
- 1:04Speaker 1: is to pull back the curtain. We want to turn
- 1:07Speaker 1: those frustrating you know, keyboard smashing gotchas into real aha moments,
- 1:12Speaker 1: so you can write highly predictable code.
- 1:14Speaker 2: And you really need that predictability because JavaScript is everywhere.
- 1:18Speaker 2: You're probably dealing with constant information overload, just trying to
- 1:22Speaker 2: keep up with the newest framework.
- 1:23Speaker 1: Well there's a new one every week, right.
- 1:25Speaker 2: But the libraries change every year, the core mechanics do not.
- 1:29Speaker 2: So by taking a structured, respectful pace today, we really
- 1:33Speaker 2: want to cut through all that noise and deliver the
- 1:35Speaker 2: underlying reasons why JavaScript behaves the way it does.
- 1:38Speaker 1: Okay, let's unpack this. We have to start at the
- 1:40Speaker 1: absolute foundation. If you don't understand how a language interprets
- 1:45Speaker 1: basic data, I mean, nothing else built on top of
- 1:48Speaker 1: it will make any sense. Yeah, the data is everything,
- 1:50Speaker 1: and our source material drops a major bombshell right in
- 1:53Speaker 1: chapter one. JavaScript has no true integers.
- 1:56Speaker 2: None, zero, none, whatsoever.
- 1:59Speaker 1: That is why to me, it is.
- 2:01Speaker 2: Every single time you type a one or a forty two,
- 2:05Speaker 2: or like a negative five, JavaScript is actually storing it
- 2:08Speaker 2: as a sixty four bit double precision floating point number. Okay, wait,
- 2:12Speaker 2: it uses the the IE seven fifty four standard, which
- 2:16Speaker 2: is basically just a fancy way of saying it stores
- 2:19Speaker 2: everything as a decimal in binary format.
- 2:21Speaker 1: So even a perfectly whole number like a solid integer,
- 2:24Speaker 1: is secretly carrying decimal point baggage.
- 2:27Speaker 2: Yes, exactly, and this is exactly why your point one
- 2:30Speaker 2: plus point two equation fails.
- 2:32Speaker 1: Okay, break that down for me.
- 2:34Speaker 2: Think about how we do math in base ten like
- 2:36Speaker 2: normal decimals. If you try to write the fraction one
- 2:38Speaker 2: third is decimal, you literally can't do it perfectly. You
- 2:41Speaker 2: get point three three three repeating forever.
- 2:43Speaker 1: Right. Eventually you just have to chop it off and
- 2:45Speaker 1: you lose a tiny bit of.
- 2:46Speaker 2: Precision exactly because base ten can't cleanly divide by three. Well, binary,
- 2:50Speaker 2: which is base two, has that exact same problem with
- 2:52Speaker 2: fractions like one tenth. It cannot cleanly represent point one.
- 2:56Speaker 1: Oh so it creates a repeating decimal in binary.
- 2:59Speaker 2: Yes, it creates repeating binary decimal, and the computer has
- 3:02Speaker 2: to chop it off at sixty four bits, which leaves
- 3:04Speaker 2: this microscopic fraction of a remainder. Oh wow, So when
- 3:07Speaker 2: you add two of those imprecise binary decimals together, that
- 3:10Speaker 2: tiny little remainder overflows into your visible result, and boom,
- 3:14Speaker 2: you get that bizarre point three zero zero zero zero
- 3:18Speaker 2: zero zero zero zero zero zero zero zero.
- 3:21Speaker 1: Four that makes so much sense. It's not a bug
- 3:23Speaker 1: at all. It's just a fundamental limitation of trying to
- 3:26Speaker 1: map base ten fractions onto base two hardware.
- 3:29Speaker 2: Exactly.
- 3:30Speaker 1: Well, wait, let me logic through something here. If every
- 3:33Speaker 1: single number is a floating point, decimal bitwise operator shouldn't
- 3:36Speaker 1: even work. I mean, you can't flip binary bits on
- 3:39Speaker 1: a floating decimal point. Right, bitwise math inherently relies on
- 3:43Speaker 1: strict thirty two bit integers.
- 3:45Speaker 2: You are entirely correct. Technically, you shouldn't be able to
- 3:48Speaker 2: do it. But JavaScript does something incredibly sneaky behind your
- 3:51Speaker 2: back to make it work anyway, of course it does. Yeah,
- 3:53Speaker 2: So when you use a bitwise operator, the engine silently
- 3:55Speaker 2: takes your sixty four big float, It actually converts it
- 3:58Speaker 2: into a thirty two bit interature, performs a bitwise math
- 4:01Speaker 2: on it, and then secretly converts the result back into
- 4:03Speaker 2: a standard sixty four bit floating point.
- 4:06Speaker 1: Are you serious?
- 4:07Speaker 2: Yeah? It all happens completely invisibly and just a fraction
- 4:09Speaker 2: of a millisecond.
- 4:10Speaker 1: Talk about doing things behind my back. That actually brings
- 4:14Speaker 1: us to another major theme in Herman's book, implicit coercion.
- 4:18Speaker 2: Oh yeah, the engine's favorite party trick.
- 4:20Speaker 1: Seriously, the engine is just constantly trying to be forgiving.
- 4:24Speaker 1: For example, the book points out that if you write
- 4:27Speaker 1: the number three plus the boole and true, JavaScript just
- 4:30Speaker 1: decides sure true is basically one, so that equals four.
- 4:33Speaker 1: It just guesses right, or the plus operator being completely overloaded,
- 4:38Speaker 1: like if you add the string two and the number
- 4:40Speaker 1: three breaks the tie and favor strings and just gives
- 4:43Speaker 1: you the string twenty three.
- 4:44Speaker 2: Yeah, it acts like this hyper eager assistant. It sees
- 4:48Speaker 2: a typo or a type mismatch, and instead of just
- 4:51Speaker 2: stopping everything to ask you what you meant, it just
- 4:53Speaker 2: guesses based on this really rigid, hard coded set of rules.
- 4:58Speaker 1: You know, I honestly want to push back on the
- 5:00Speaker 1: author a little bit here.
- 5:01Speaker 2: Okay, go for it.
- 5:02Speaker 1: If the language is genuinely trying to be helpful, why
- 5:04Speaker 1: shouldn't we just let it coerce things? Doesn't it save
- 5:07Speaker 1: us time? I mean, if I tap a string of two,
- 5:10Speaker 1: the computer converting it for me, feels like one less
- 5:12Speaker 1: line of code I have to write I.
- 5:14Speaker 2: Get that, but consider the alternative when that eger assistant
- 5:19Speaker 2: guess is wrong, like semantics usually fail because computers simply
- 5:23Speaker 2: cannot read minds. Fair point. Let's say you have a
- 5:26Speaker 2: variable that's supposed to be a number. Right, You're calculating
- 5:29Speaker 2: a user's cart total on an e commerce site, but
- 5:32Speaker 2: due to some weird bug in an API, it accidentally
- 5:36Speaker 2: becomes null. Okay, not ideal in a stricter language. The
- 5:40Speaker 2: very moment you tried to add a price to null,
- 5:42Speaker 2: the program completely crashes. Y. It stops and tells you
- 5:46Speaker 2: exactly where the error happened. But in JavaScript, JavaScript just
- 5:50Speaker 2: silently coerces null to zero. Oh no, yeah, it just
- 5:54Speaker 2: completes the math. So now your calculation is entirely wrong.
- 5:58Speaker 2: That bad data propagates all the way through your day database,
- 6:00Speaker 2: and your user gets charged the wrong amount. That's horrible,
- 6:03Speaker 2: And the worst part is the error doesn't happen where
- 6:05Speaker 2: the actual mistake was made. It happens like miles down
- 6:08Speaker 2: the road. It makes debugging an absolute nightmare because implicit
- 6:12Speaker 2: coercion hides your type bears instead of catching them.
- 6:15Speaker 1: Okay, yeah, that is a terrifying scenario, which, honestly it
- 6:18Speaker 1: leads us perfectly into Nan not a number. Ugh.
- 6:21Speaker 2: Nan the most ironically named value in the entire language.
- 6:24Speaker 1: Right, considering the type of operator literally classifies Nan as
- 6:27Speaker 1: a number.
- 6:28Speaker 2: It's hilarious. It's a number that means not a number.
- 6:32Speaker 1: And testing for it is notoriously broken. The book mentions
- 6:35Speaker 1: that if you use the strict equality operator nanin equals
- 6:39Speaker 1: so nan actually evaluates to false. Yep, if a value
- 6:42Speaker 1: isn't equal to itself, how do you reliably check for
- 6:45Speaker 1: it without just pulling your hair out?
- 6:47Speaker 2: Well, the author actually provides a brilliant, super reliable trick
- 6:50Speaker 2: for this. You check if a value is unequal to itself.
- 6:53Speaker 2: Wait what Yeah, So if you write x equals x
- 6:56Speaker 2: and that specific statement evaluates to true, then x is
- 7:00Speaker 2: absolutely guaranteed to be nan. Because of that IEE standard
- 7:03Speaker 2: we talked about earlier, Nan is literally the only value
- 7:06Speaker 2: in the entire language where that inequality holds true.
- 7:09Speaker 1: Wow, that is a phenomenal nugget of knowledge, and it
- 7:12Speaker 1: ties right back to why we have to avoid the
- 7:14Speaker 1: double equals operator and just rely on triple equals.
- 7:16Speaker 2: Always use triple equals.
- 7:17Speaker 1: Because double equals triggers that eager assistant to start coercing
- 7:21Speaker 1: types again applying this massive, condoluted table of rules. But
- 7:25Speaker 1: triple equals is strict no coercion, just a direct, honest comparison.
- 7:30Speaker 2: And using strict equality isn't just about avoiding bugs, you know,
- 7:34Speaker 2: It's about communicating your intent to whoever reads your code
- 7:37Speaker 2: six months from now. It basically says, hey, I expect
- 7:40Speaker 2: this exact type and I accept no substitutions.
- 7:43Speaker 1: Absolutely, So the engine is constantly trying to guess our
- 7:46Speaker 1: intent with data types. But this eger assistant behavior. It
- 7:50Speaker 1: doesn't stop at data. No, it actually tries to guess
- 7:53Speaker 1: the structure of your code too. If you forget punctuation,
- 7:56Speaker 1: it will literally rewrite your syntax for you.
- 7:58Speaker 2: Let's talk about automatics Colon insertion or ASI. So.
- 8:01Speaker 1: ASI was originally designed as an error correction mechanism. The
- 8:05Speaker 1: idea was if a developer accidentally leaves off a statement
- 8:08Speaker 1: terminating semicolon, the engine tries to infer where it belongs
- 8:11Speaker 1: and inserts one invisibly.
- 8:13Speaker 2: To keep the script from breaking.
- 8:15Speaker 1: But it gets it wrong. It doesn't actually read your mind.
- 8:18Speaker 1: The book highlights five specific problem characters that completely disable
- 8:22Speaker 1: this insertion, the open parenthesis, the open square bracket, the
- 8:26Speaker 1: plus sign, the minus sign, and the forward slash yeah.
- 8:30Speaker 2: And to visualize this like without looking at a screen,
- 8:34Speaker 2: imagine you write a variable assignment online one, just let
- 8:37Speaker 2: result equals a plus b. Yeah, but you forget the semicolon.
- 8:40Speaker 1: Okay.
- 8:41Speaker 2: Then online two you decide to group some completely separate
- 8:44Speaker 2: map together using an open parenthesis. So line two just
- 8:47Speaker 2: starts with X plus y, right.
- 8:49Speaker 1: And because JavaScript doesn't see a semi coolon online one,
- 8:52Speaker 1: it looks at the open parenthesis online two and assumes
- 8:54Speaker 1: they are connected. It thinks you are trying to call
- 8:57Speaker 1: a plus b as a function and passing x plus
- 8:59Speaker 1: one as the argument.
- 9:00Speaker 2: Exactly. It just jams the two lines together and you
- 9:03Speaker 2: get this completely baffling not a function error. And there's
- 9:06Speaker 2: actually a secondary trap here called restricted productions, where it
- 9:10Speaker 2: does the exact.
- 9:10Speaker 1: Operation where it forcefully inserts a semicolone where you explicitly
- 9:14Speaker 1: did not want one, exactly, a classic return statement bug.
- 9:17Speaker 2: Yes, this gets everyone. Imagine you write the keyword return,
- 9:22Speaker 2: and because you want your code to look really clean
- 9:24Speaker 2: and readable, you hit enter and put the object you're
- 9:27Speaker 2: returning on the next line down makes sense. The JavaScript
- 9:30Speaker 2: engine sees return, hits the new line, and instantly says
- 9:34Speaker 2: oh statement over and just drops an invisible semicolon right
- 9:37Speaker 2: after return.
- 9:38Speaker 1: So your function just silently returns undefined. Yep, and that
- 9:42Speaker 1: beautifully formatted object sitting on the next line is just
- 9:45Speaker 1: dead code. It will never be reached, never invisible bugs.
- 9:48Speaker 1: It's honestly fascinating.
- 9:50Speaker 2: It forces you to realize that white space and new
- 9:52Speaker 2: lines are not just aesthetic in JavaScript like they actively
- 9:56Speaker 2: participate in how the compiler interprets your logic.
- 9:59Speaker 1: Which is a huge mental shift. Yeah, and this invisible
- 10:02Speaker 1: shifting of code brings us to another massive structural quirk
- 10:06Speaker 1: variable hoisting.
- 10:07Speaker 2: Oh hoisting.
- 10:08Speaker 1: JavaScript lacks block scoping, well except for catch blocks. In
- 10:11Speaker 1: a lot of other languages, if you declare a variable
- 10:14Speaker 1: inside an if statement or a for loop, it only
- 10:16Speaker 1: exists within those brackets. It is contained, right But.
- 10:19Speaker 2: In JavaScript using the traditional var keyword, that containment just
- 10:24Speaker 2: doesn't exist. If you declare varx equals five deeply nested
- 10:28Speaker 2: inside a loop, the engine implicitly moves or hoists the
- 10:33Speaker 2: declaration of x to the very top of its enclosing function.
- 10:36Speaker 1: But just the declaration right, not the assignment.
- 10:39Speaker 2: Correct, So the space in memory is created at the top,
- 10:43Speaker 2: but the value of five isn't actually assigned until the
- 10:45Speaker 2: engine runs down to the specific line where you originally
- 10:48Speaker 2: wrote it.
- 10:48Speaker 1: Okay, here's an analogy for hoisting. It's like a stage
- 10:51Speaker 1: director taking all your character introductions and moving them to
- 10:54Speaker 1: the very first scene of the play so the audience
- 10:56Speaker 1: knows they exist. I like this, but the director leaves
- 11:00Speaker 1: their actual costume changes and prop assignments exactly where you
- 11:04Speaker 1: originally wrote them in X two.
- 11:06Speaker 2: Yeah, the director moves the actors but leaves their props behind,
- 11:09Speaker 2: so if they try to use that prop and scene one,
- 11:12Speaker 2: the result is just undefined exactly. And this is why
- 11:14Speaker 2: hoisting causes so many daily bugs. If you don't realize
- 11:18Speaker 2: a loop variable is being hoisted to the top of
- 11:20Speaker 2: your function, you might accidentally overwrite another variable with the
- 11:24Speaker 2: exact same name higher.
- 11:25Speaker 1: Up, which is exactly why the author emphasizes manual hoisting
- 11:29Speaker 1: as a best practice. Just literally write all your VAW
- 11:32Speaker 1: declarations at the very top of your function manually.
- 11:35Speaker 2: Yeah, be your own stage director.
- 11:37Speaker 1: Right, because it visually matches how the engine actually interprets
- 11:40Speaker 1: the code, which saves you from those crazy loop variable
- 11:44Speaker 1: overwrite bugs. You are basically organizing the actors before the
- 11:48Speaker 1: play begins.
- 11:49Speaker 2: It really is about writing code the way the compiler
- 11:52Speaker 2: reads it. When your mental model matches the engine's mechanical model,
- 11:56Speaker 2: the bugs just disappear.
- 11:57Speaker 1: So true. Okay, so hoisting shows us how jobscript moves
- 12:00Speaker 1: variable declarations around in a function's scope. But what happens
- 12:05Speaker 1: when functions hold onto those variables across time?
- 12:07Speaker 2: Now we're getting to the good stuff.
- 12:09Speaker 1: Yeah, this brings us to what might be the language's
- 12:11Speaker 1: most powerful expressive feature closures.
- 12:14Speaker 2: Closures are incredibly elegant once you strip away all the
- 12:17Speaker 2: computer science jargon. In really simple terms, a closure allows
- 12:22Speaker 2: a function to remember variables from its outer scope even
- 12:25Speaker 2: after that outer function has completely finished executing. In return,
- 12:29Speaker 2: it's like.
- 12:29Speaker 1: A memory that outlives the event itself.
- 12:32Speaker 2: That's a great way to put it.
- 12:33Speaker 1: But here is the crucial, often misunderstood detail from Herman's book.
- 12:38Speaker 1: Closures store references to their outer variables. They do not
- 12:43Speaker 1: store copies of their values at that frozen moment in time.
- 12:46Speaker 2: Yes, and this specific detail trips up seasoned developers constantly
- 12:51Speaker 2: because it's a reference. It means the closure is looking
- 12:54Speaker 2: at a live variable.
- 12:55Speaker 1: Okay, give me an example.
- 12:57Speaker 2: Think about a for loop that runs three times from
- 13:00Speaker 2: zero to two. Inside that loop, you create three different
- 13:04Speaker 2: button click functions, and each one is supposed to print
- 13:07Speaker 2: the current loop number.
- 13:08Speaker 1: Okay, so you would logically expect the first button to
- 13:10Speaker 1: print zero, the second to print one, and the third
- 13:13Speaker 1: to print two naturally, but.
- 13:15Speaker 2: They don't capture a frozen copy of the number. They
- 13:17Speaker 2: capture a live reference to the single variable eye.
- 13:19Speaker 1: Oh.
- 13:20Speaker 2: So, by the time the loop is finished running, I
- 13:22Speaker 2: is incremented to its maximum value, which is three. Later,
- 13:26Speaker 2: when a user finally clicks one of those buttons, the
- 13:29Speaker 2: closure looks up the current value of IE, and every
- 13:31Speaker 2: single button just prints three.
- 13:33Speaker 1: That is so frustrating. So to fix it, you basically
- 13:36Speaker 1: have to freeze time for each iteration. You can't just
- 13:39Speaker 1: let the closure point to the main loop variable, right, You.
- 13:42Speaker 2: Need to force a brand new, completely isolated local scope
- 13:46Speaker 2: for every single iteration. And the classic JavaScript workaround for
- 13:50Speaker 2: this is using an IIE an immediately invoked function expression.
- 13:55Speaker 1: Okay, let's slow that down for the listener. How exactly
- 13:57Speaker 1: does an IE freeze time?
- 13:59Speaker 2: Okay, so instead of just creating the closure directly in
- 14:01Speaker 2: the loop, you wrap it inside a completely separate temporary function.
- 14:06Speaker 2: You pass the current loop number into that temporary function
- 14:08Speaker 2: as an argument, and you command the engine to run
- 14:11Speaker 2: it right then and there.
- 14:12Speaker 1: Ah immediately invoked exactly.
- 14:15Speaker 2: That temporary function locks the value in place for that
- 14:18Speaker 2: specific iteration, and then returns your closure safely sealed inside
- 14:21Speaker 2: that frozen environment.
- 14:23Speaker 1: Okay, Wait, I have an insight here. I want to
- 14:25Speaker 1: test with you. Sure, if closures store live references rather
- 14:28Speaker 1: than static values, doesn't that mean multiple closures can actually
- 14:32Speaker 1: communicate by modifying the exact same outer variable.
- 14:36Speaker 2: Yes, you just hit on one of the most powerful
- 14:39Speaker 2: architectural patterns in the language. Really, yeah, it's not a
- 14:42Speaker 2: bug at all, it's a massive superpower. This is the
- 14:45Speaker 2: foundation of the box pattern. Imagine a function that defines
- 14:50Speaker 2: a single private variable, say a bank account balance. Okay,
- 14:55Speaker 2: then that function returns an object containing two closures, a
- 14:59Speaker 2: deposit function and a withdraw function.
- 15:01Speaker 1: And because they were both created in the same outer scope,
- 15:04Speaker 1: they share the exact same live reference to that one
- 15:08Speaker 1: balance variable.
- 15:09Speaker 2: Precisely, so, if the deposit closure adds money, the withdraw
- 15:13Speaker 2: closure will instantly see the new balance because they are
- 15:15Speaker 2: looking at the exact same space in memory.
- 15:17Speaker 1: Wow. And because that balance variable was declared inside a
- 15:21Speaker 1: function that has already returned, it is completely invisible to
- 15:25Speaker 1: the rest of the program.
- 15:25Speaker 2: Yep, untouchable.
- 15:26Speaker 1: You get true encapsulation, true private state without needing traditional
- 15:30Speaker 1: classes or complex architecture. The outside world can only touch
- 15:34Speaker 1: the balance using the specific closures you explicitly handed back.
- 15:37Speaker 2: It's brilliant, lightweight encapsulation. It's what makes JavaScript so unique.
- 15:41Speaker 1: I love that. So closures are all about how functions
- 15:46Speaker 1: capture their surrounding environment. But a function's identity itself changes
- 15:51Speaker 1: entirely depending on exactly how you invoke it.
- 15:53Speaker 2: Oh boy, Yeah, I'm talking.
- 15:54Speaker 1: About the chameleon nature of functions and the dreaded this keyword.
- 15:58Speaker 2: The binding of this is arguable the single biggest source
- 16:01Speaker 2: of hair pulling in JavaScript history.
- 16:03Speaker 1: I've lost a few hairs to it myself.
- 16:04Speaker 2: We all have. And the confusion stems from treating JavaScript
- 16:08Speaker 2: functions like they are static. They aren't. You have to
- 16:11Speaker 2: understand the difference between calling a function, calling a method,
- 16:15Speaker 2: and calling a constructor.
- 16:16Speaker 1: But it can be the exact same block of code,
- 16:19Speaker 1: literally the exact same text on the screen.
- 16:21Speaker 2: I know, but the text doesn't matter. The syntax of
- 16:23Speaker 2: the invocation changes everything. When you invoke code, the engine
- 16:27Speaker 2: assigns a receiver to the keyword this.
- 16:29Speaker 1: Okay.
- 16:30Speaker 2: If you call it purely as a standard function like
- 16:32Speaker 2: just hello, this binding defaults to pointing at the global object,
- 16:39Speaker 2: or it becomes undefined. If you're operating in strict mode.
- 16:42Speaker 1: That feels incredibly dangerous. If you accidentally use this thinking
- 16:46Speaker 1: you are modifying a local property, you could be accidentally
- 16:50Speaker 1: overwriting global variables.
- 16:51Speaker 2: You absolutely could, and people do. Now. If you call
- 16:54Speaker 2: it as a method, meaning it is attached to an
- 16:57Speaker 2: object like user dot hello, then this is securely bound
- 17:01Speaker 2: to the user object. The object itself becomes the receiver.
- 17:05Speaker 1: Okay, that makes sense. And what if you call it
- 17:06Speaker 1: with the new keyword like new hello.
- 17:09Speaker 2: Then the function completely transforms into a constructor. The engine
- 17:13Speaker 2: creates a brand new empty object on the fly and
- 17:16Speaker 2: binds this to that new object for the duration of
- 17:19Speaker 2: the function.
- 17:19Speaker 1: Okay, here's my analogy for this chameleon behavior. Tell me
- 17:22Speaker 1: if this works.
- 17:23Speaker 2: Let's here it.
- 17:23Speaker 1: A JavaScript function is like a highly versatile actor. If
- 17:27Speaker 1: you call it normally, the actor is performing out on
- 17:29Speaker 1: the street that's the global object. Anything goes usually chaos. Sure,
- 17:34Speaker 1: if you call it as a method, they are performing
- 17:36Speaker 1: on a specific set, like a living room. And if
- 17:38Speaker 1: you use new the engine builds them a brand new
- 17:41Speaker 1: stage right in front of your eyes.
- 17:42Speaker 2: I think that's a perfect way to visualize it.
- 17:45Speaker 1: But what if I want to guarantee where they perform?
- 17:48Speaker 1: What if I extract a method and pass it around.
- 17:51Speaker 2: Ah, that is where the danger peaks. Think about passing
- 17:55Speaker 2: a method as a callback. You have user dot log in,
- 17:59Speaker 2: and you pass that to a higher order function like
- 18:01Speaker 2: an arraise dot for each.
- 18:03Speaker 1: Happens all the time, right, But.
- 18:04Speaker 2: When you hand user dot log in too for each,
- 18:08Speaker 2: you aren't handing it the user object. You are just
- 18:10Speaker 2: handing it the raw unattached function.
- 18:13Speaker 1: Oh you strip away the receiver exactly.
- 18:15Speaker 2: When four each eventually calls your callback, it calls it
- 18:18Speaker 2: as a plane unattached function. The actor is back on
- 18:21Speaker 2: the street.
- 18:22Speaker 1: Oh wow.
- 18:22Speaker 2: Yeah, So this suddenly points to the global object and
- 18:25Speaker 2: your program crashes because the login properties it expects simply
- 18:29Speaker 2: aren't there.
- 18:30Speaker 1: So, if you, the listener, have ever passed a callback
- 18:32Speaker 1: to a higher order function and watched your application basically
- 18:35Speaker 1: catch fire, because this suddenly pointed to the wrong place.
- 18:38Speaker 1: This is why you lost your receiver.
- 18:40Speaker 2: Yep.
- 18:41Speaker 1: So how do we fix the actor's contract? How do
- 18:43Speaker 1: we stop them from going back to the street.
- 18:45Speaker 2: You use the dot bind method. Calling dot bind on
- 18:48Speaker 2: a function creates a new wrapper function that permanently locks
- 18:52Speaker 2: down the receiver.
- 18:52Speaker 1: So it gives the actor an ironclad contract to only
- 18:55Speaker 1: perform in one specific theater, no matter who hires them
- 18:58Speaker 1: or when they get called later. You safely lock this
- 19:01Speaker 1: in place.
- 19:02Speaker 2: Yes, is the ultimate safeguard for callbacks.
- 19:05Speaker 1: That's incredibly useful. Now, understanding functions as constructors. Using that
- 19:09Speaker 1: new keyword naturally leads us to our final destination today.
- 19:14Speaker 1: How JavaScript actually builds objects and shares behavior.
- 19:17Speaker 2: The prototype system.
- 19:18Speaker 1: Yes, we are ditching traditional blueprints for something far more dynamic.
- 19:22Speaker 1: Let's talk about prototypes.
- 19:24Speaker 2: This requires a major mental shift. JavaScript does not have
- 19:28Speaker 2: built in classes in the traditional object oriented sense like
- 19:31Speaker 2: Java or C plus plus man.
- 19:33Speaker 1: Which shocks a lot of people.
- 19:34Speaker 2: It really does. It relies entirely on prototypes, which is
- 19:38Speaker 2: a system of implementation inheritance through dynamic delegation.
- 19:42Speaker 1: Okay, before we explain delegation, let's untangle a massive naming
- 19:47Speaker 1: overlap that the book brings up because I know it
- 19:48Speaker 1: confuses everyone the difference between the propertyconstructor dot prototype and
- 19:54Speaker 1: the function object dot get.
- 19:56Speaker 2: Prototypeo Oh, it's a naming disaster, honestly, it really is.
- 19:59Speaker 2: So when you see a property called dot prototype on
- 20:02Speaker 2: a constructor function dash like user about prototype, that is
- 20:06Speaker 2: not the prototype of the user function itself.
- 20:09Speaker 1: Wait?
- 20:09Speaker 2: Really no, that is the object that will be assigned
- 20:13Speaker 2: as the prototype for all future objects created when you
- 20:15Speaker 2: eventually call new user.
- 20:17Speaker 1: Ah. Okay, so it's the template for the future generation.
- 20:20Speaker 2: Yes, but if you want to find the prototype of
- 20:23Speaker 2: an existing, already created object, you don't look for a
- 20:26Speaker 2: dot prototype property. You have to use the standard built
- 20:28Speaker 2: in function object dot get prototype of and pass your
- 20:31Speaker 2: existing object right into it. Got it?
- 20:33Speaker 1: Okay, So template for the future versus checking the current reality.
- 20:36Speaker 2: Now.
- 20:37Speaker 1: The book heavily emphasizes why we must store our methods
- 20:40Speaker 1: on these prototypes rather than just putting them inside the
- 20:42Speaker 1: constructor function itself. Why is that so important?
- 20:45Speaker 2: It all comes down to sheer memory efficiency. Imagine you
- 20:49Speaker 2: are building a game, right and you put a method
- 20:52Speaker 2: inside the constructor just not attack function and so on.
- 20:55Speaker 2: Pretty standard, but every single time you create a new
- 20:59Speaker 2: enemy instance, the JavaScript engine allocates fresh memory for a
- 21:04Speaker 2: brand new function object. Yeah, if you spawn ten thousand enemies,
- 21:08Speaker 2: you have ten thousand identical function objects just clogging up
- 21:12Speaker 2: your computer's memory.
- 21:13Speaker 1: Oh that's incredibly wasteful. But if you put the attack
- 21:16Speaker 1: method on the prototype, you.
- 21:17Speaker 2: Stored in memory exactly once, just once, just once. All
- 21:20Speaker 2: ten thousand enemy objects are completely empty of methods, but
- 21:24Speaker 2: they all have a hidden link to that single prototype object.
- 21:28Speaker 2: So when you call enemy dot attack, the object says,
- 21:31Speaker 2: I don't have that method here. Let me dynamically delegate
- 21:33Speaker 2: this request up to my prototype, and.
- 21:35Speaker 1: It finds the shared method exactly.
- 21:36Speaker 2: It finds a single shared method and runs it. It's
- 21:38Speaker 2: incredibly lean.
- 21:39Speaker 1: That delegation is beautiful. But I have a fundamental question here.
- 21:43Speaker 1: If prototypes are so radically different from classes, and they're
- 21:46Speaker 1: so incredibly memory efficient, why do programmers constantly try to
- 21:50Speaker 1: force class like syntax and strict hierarchies onto JavaScript. Isn't
- 21:55Speaker 1: dynamic delegation actually simpler once you just stop fighting it.
- 21:59Speaker 2: It's just human nature, you know. We gravitate toward what
- 22:02Speaker 2: we know. The vast majority of developers cut their teeth
- 22:05Speaker 2: on Java C sharp or C plus plus A, where
- 22:09Speaker 2: static rigid classes are literally the only way to model
- 22:12Speaker 2: the world, it feels safe. Shifting to JavaScript requires unlearning
- 22:17Speaker 2: some deeply ingrained habits.
- 22:18Speaker 1: You really have to let go of the bluepress.
- 22:20Speaker 2: You do, But once you actually embrace prototypal delegation, you
- 22:23Speaker 2: realize it's this deeply flexible system. It allows you to
- 22:27Speaker 2: easily clone objects, swap out behaviors on the fly, and
- 22:29Speaker 2: just mix and match properties without the rigid bureaucracy of
- 22:33Speaker 2: massive class taxonomies.
- 22:34Speaker 1: That sounds so freeing it is.
- 22:36Speaker 2: You aren't building inescapable rigid family trees. You're just dynamically
- 22:40Speaker 2: linking objects to other objects.
- 22:42Speaker 1: So what does this all mean for you, the listener?
- 22:44Speaker 1: Let's recap this journey. We have systematically stripped away the
- 22:47Speaker 1: illusions of JavaScript today.
- 22:49Speaker 2: We covered a lot of ground we really did.
- 22:52Speaker 1: From those pseudo integers causing binary math failures to the
- 22:55Speaker 1: sneaky invisible semicolons rewriting your syntax behind your back. We
- 23:00Speaker 1: uncovered how variable horsting moves your actors around, how closures
- 23:05Speaker 1: capture live references to create private state, The chameleon nature
- 23:09Speaker 1: of this keyword and finally, the memory saving power of
- 23:13Speaker 1: classless prototypes.
- 23:14Speaker 2: Yeah, we now see the language for what it truly
- 23:17Speaker 2: is beneath the surface exactly.
- 23:19Speaker 1: It's a language that demands respect for its internal mechanics.
- 23:22Speaker 1: You simply cannot just skim the surface and expect your
- 23:25Speaker 1: code to scale predictably.
- 23:27Speaker 2: You really can't. It'll catch up with you.
- 23:29Speaker 1: It demands that respect. And I want to leave you
- 23:31Speaker 1: with one final provocative thought to explore on your own.
- 23:35Speaker 1: We just talked about checking and setting prototypes safely using
- 23:38Speaker 1: standard methods, But lurking in the shadows of JavaScript is
- 23:42Speaker 1: a dark, non standard property called proto that is proto
- 23:45Speaker 1: spelled with two underscores on each side.
- 23:47Speaker 2: Oh wow, a truly terrifying amount of power.
- 23:50Speaker 1: It really is. Modifying an object's proto dynamically is quite
- 23:54Speaker 1: literally like giving the object a brain transplant at runtime.
- 23:57Speaker 2: Yeah, don't do it.
- 23:58Speaker 1: It completely invalidates the job. Abscript engines internal performance optimizations,
- 24:02Speaker 1: and it swaps the objects entire inheritance hierarchy on the fly.
- 24:07Speaker 1: The author of our source text warns that you should
- 24:09Speaker 1: never modify it in production CAD.
- 24:11Speaker 2: It is the ultimate dynamic temptation, best left entirely alone.
- 24:15Speaker 1: Let the shadow remain a shadow, just something fascinating to ponder.
- 24:19Speaker 1: The next time you think you are in complete safe control.
- 24:22Speaker 2: Of your objects, good luck out there.
- 24:24Speaker 1: So the next time you are debugging and things get murky,
- 24:27Speaker 1: remember the diagnostic X ray machine might be broken, but
- 24:30Speaker 1: now you understand the physics of the machine itself. Thanks
- 24:33Speaker 1: for taking the deep dive with us.