treepg.blogg.se

You dont know js confusing
You dont know js confusing









Yes I wrote the examples on the console of my browser when I thought they are confusing but for the most of the times there was no need.

you dont know js confusing

If you already know a programming language it should take you much less. Maybe I would spent 4 hours per day on that book on average. I think that took 3-4 months doing nothing more than that (I do not have a job). This kind of closure illustration typically uses a setTimeout(.) or some other callback like an event handler, inside the loop.I have read around 70% of YDKJS (all the books combined). outer/global scope: RED(1) function lookupStudent ( studentID ) keeps ( ) // 3 - WHY!? keeps ( ) // 3 keeps ( ) // 3 NOTE: Let's look at some code, annotated with its relevant scope bubble colors (see Chapter 2): A function executing in the same scope it was defined would not exhibit any observably different behavior with or without closure being possible by the observational perspective and definition, that is not closure. Only functions have closure.įor closure to be observed, a function must be invoked, and specifically it must be invoked in a different branch of the scope chain from where it was originally defined. An object cannot have closure, nor does a class have closure (though its functions/methods might). If you aren't dealing with a function, closure does not apply. However, later in this chapter, we're going to flip closure around to look at it from an alternative perspective.Ĭlosure is a behavior of functions and only functions. We'll start by defining closure in terms of what we can observe in different behavior of our programs, as opposed to if closure was not present in JS. Instead, I'm going to focus on a practical perspective. But I'm not going to list out math formulas or use a bunch of notation and jargon to define it. See the ClosureĬlosure is originally a mathematical concept, from lambda calculus. Make sure to take your time and ensure you're comfortable with each bit before moving onto the next.

#YOU DONT KNOW JS CONFUSING CODE#

Getting comfortable with closure is required for mastering JS and effectively leveraging many important design patterns throughout your code.Īddressing all aspects of closure requires a daunting mountain of discussion and code throughout this chapter. guess what!? That's closure.Ĭlosure is one of the most important language characteristics ever invented in programming-it underlies major programming paradigms, including Functional Programming (FP), modules, and even a bit of class-oriented design. If you've ever written a callback that accesses variables outside its own scope. We already saw an example of this kind of closure in the previous chapter ( factorial(.) in Chapter 6), and you've almost certainly already used it in your own programs. Functions remember these referenced scoped variables via closure. This helps keep code understandable and maintainable, and helps avoid many scoping pitfalls (i.e., name collision, etc.).Ĭlosure builds on this approach: for variables we need to use over time, instead of placing them in larger outer scopes, we can encapsulate (more narrowly scope) them but still preserve access from inside functions, for broader use. Recall the main conclusion of Chapter 6: the least exposure principle (POLE) encourages us to use block (and function) scoping to limit the scope exposure of variables. Our broad goal in this book is not merely to understand scope, but to more effectively use it in the structure of our programs closure is central to that effort. Don't worry! You don't need an advanced computer science degree to make sense of it.

you dont know js confusing you dont know js confusing

Our attention again shifts broader in abstraction, to the historically somewhat daunting topic of closure. Up to this point, we've focused on the ins and outs of lexical scope, and how that affects the organization and usage of variables in our programs. You Don't Know JS Yet: Scope & Closures - 2nd Edition Chapter 7: Using Closures









You dont know js confusing