No matter how much experience you have with JavaScript, odds are you don't fully understand the language. This concise yet in-depth guide takes you inside scope and closures, two core concepts you need to know to become a more efficient and effective JavaScript programmer. You'll learn how and why they work, and how an understanding of closures can be a powerful part of your development skillset.
Like other books in the You Don't Know JS series, Scope and Closures dives into trickier parts of the language that many JavaScript programmers simply avoid. Armed with this knowledge, you can achieve true JavaScript mastery.
Learn about scope, a set of rules to help JavaScript engines locate variables in your code Go deeper into nested scope, a series of containers for variables and functions Explore function- and block-based scope, "hoisting", and the patterns and benefits of scope-based hiding Discover how to use closures for synchronous and asynchronous tasks, including the creation of JavaScript libraries
Kyle Simpson is an Open Web Evangelist from Austin, TX, who's passionate about all things JavaScript. He's an author, workshop trainer, tech speaker, and OSS contributor/leader.
What a great little book. This might be the first Javascript book I've managed to finish without having my eyes glaze over from the tedium. Modern technical books tend to lean far too much on how-to examples and not enough time on deeper explanations. This book focuses on one particular aspect of the language, scope rules and closures, and explains it in depth. The author understands how important well-crafted analogies and mental models are for achieving genuine understanding.
Marvelous book. Kyle deserves every cent received at Kickstarter.
The book is so concise, that it seems like every word is brushed until it shines, revealing the underlying truth about JavaScript and it's key differences from other languages.
Topics covered: scope, different types of scope, modules, IFFE, hoisting, closures, polyfilling. Code samples are short, intention revealing and very informative.
I've been through two books in this series and very thrilled to dive deeper into next books.
Thank you, Kyle, for showing us, that we might not understand the language we think we are and we just might need to spend some extra effort in order to effectively apply JavaScript.
الكتاب الثاني من سلسلة أنت لا تعرف جافاسكربت إن كنتَ تبحث عن كتاب تتعلم من خلاله جافاسكربت يمكنك قراءة مراجعتي لكتاب هيدفيرست هنا *
كما يبدو واضحًا من عنوانه يتناول هذا الكتاب مفهومي النطاق ودالات الإغلاق - إن صحّت الترجمة - بشكل مُعمق ومُثقف، مستندًا بالشرح بشكل أساسي إلى الطريقة التي يعمل بها مترجم اللغة compiler ** والقواعد التي يتبعها في معرفة أين وكيف يمكن الوصول إلى المتغيرات والدوال التي يتم تعريفها.
يعرض المؤلف كذلك بعض الطرق التي تشيع بين مبرمجي جافاسكربت كنوع من خداع القواعد التي يتبعها المترجم في بناء النطاقات، مثل دوال eval & with ويعرض لمساوئ استخدام هاتين الدالتين وبدائلهما
نصف الكتاب الثاني مخصص لعرض مفهومي Hoisting & Closure وهي مفاهيم مفتاحية في فهم جافاسكربت بشكل أعمق.
أعجبتني التفاصيل والملاحظات الدقيقة التي يذكرها المؤلف على امتداد الكتاب، والتي تجد معظم المواقع التعليمية وكورسات الفيديو تجملها كثيرًا أو تتجاهلها ببساطة. ولكن لم أحب أسلوبه الشخصي عند تهويل نقطة ما، هذا الأسلوب كان يستخدمه بعض الأساتذة في المدرسة فمثلا قبل شرح مفهوم ما، يقول لك هذا المفهوم صعب جدا، وفهمه يعتبر أمر قليل بين المبرمجين ولكن يجب أن تفهمه وأن تحاول حتى تصل إلى لحظة التنوير التي تدرك بها عن ماذا نتحدث تماما! في المقابل وفي كتاب هيدفيرست، المؤلف كان يستدرج القارئ تدريجيا وبعد أن يفرغ من شرح مفهوم الكلوجر يقول له ما تعملته الآن يسمى كذا وذلك لتجنيب القارئ أي حاجز نفسي عن ادارك مفهوم ما لمجرد أن هالة تصعيبية تنتشر عنه على الشبكة.
Bardzo fajna książką skupiona na konkretnych funkcjonalnościach JavaScript jakimi są zakresy oraz domknięcia. Ich dobra znajomość jest o tyle ważna, iż pozwoli nam napisać lepszy, wydajniejszy kod a także uniknąć różnego rodzaju błędów związanych z nie do końca oczywistym działaniem JavaScript. Tym bardziej jeśli jesteśmy programistami jakiegoś silnie typowanego języka typu C# czy Java w którym wiele rzeczy działa nieco inaczej. Gdy zdążyliśmy się do nich przyzwyczaić zderzenie z JavaScriptem może być momentami bolesne. Książka ma na celu wytłumaczyć nam ten nieco inny punkt widzenia byśmy mogli poruszać się po tym języku sprawnie i pisać zrozumiały kod. Polecam i zaraz zapewne sięgnę po inne książki tej serii.
For most newcomers to Javascript (whether they know any other programming language or not), it's scope and closure behaviour may seem very strange. This book helps you understand this topic and become a better developer
Awesome book. Helped me gain a better understanding of Scope and Closures and clear up some misconceptions! Before reading this book i actually thought JavaScript is an interpreted language but learned it is in fact a compiled language but its different from other compiled languages because compilation does not happen well in advance rather just milliseconds before execution. I also liked his definition of a closure, 'Closure is when a function is able to remember and access its lexical scope even when that function is executing outside its lexical scope.'
Kyle has strong opinions but he backs them up, whether you agree with him or not you will learn the 'why' behind some of the tricky parts in JavaScript. Looking forward to other books in the series.
I needed something to explain scopes and closures to a Junior Dev I've started working with and mentoring recently. The usual examples and code samples weren't clicking, and I began to realize that, even for me, those old stand-by's weren't what actually got me to grock closure. I had to use the module pattern extensively before I really understood the concept at an intuitive level.
This book fills in what I needed experience to understand. It is concise, it is accurate, it is clear. You can't ask for better.
Perhaps not worthy of a stand-alone title. And don't really agree with all of the advices regarding let.
The author's advice is also contradictory in the spirit of the series which is to understand deeply and then use it correctly.
On the other hand, for let the recommended use is only for block scope and even then make it explicit with braces. Ahem, that's why you use the new keyword 'let'.
If your code breaks after replacing var with let, you weren't writing very clean code in the first place.
This book was a nice refresher of things I already knew, as well as a tidy introduction to some new syntax in EC6. I appreciated his coverage of IIFEs because that's syntax I'm always very confused about. This book was explicit in its scope (hah!) - it really didn't touch on much else besides scopes and closures - but it also didn't shy away from telling you about powerful language functions just because they are not nice to think about.
Concepts such as lexical scopes, function and block scopes, closures, module pattern, IIFE and hoisting are all well explained with simple code examples. At the start, a very good intro about Javascript compilation process, its compiler and engine. Additionally, some new ES6 features are presented in conjuction with pollyfill solutions for block scopes. A very good book for those who wants to understand how JS works!
While I already considered myself somewhat of an expert when it came to scope and closure, I've read and heard Crockford talk about them a lot over the years, and Zakas books have a wealth of information on scope and closures as well I feel this book provided a new light on them. I'm a better Client Side Engineer for having read this book.
It is surprising that this good book is not too much beyond my comprehension of the language and my meager experience in the field is also a great help. Few things that I might need to go over again because I am still confused with them:
1. JS is compiled, it is not just interpreted
2. Tokenizing and lexing.
3. JS conpilation doens't work in a build step ahead of time like other language. So I guess it works in run time!
4. LHS cares about target of the assignment while RHS wants to know about the source of the assignments.
5. Function declaration is not a LHS look-up?
6. We have eval() and with that can mess up lexical scope. Then what the point of having them?
7. The Principle of Least Privilege (or Least Authority or Least Exposure)
Some amazing things that I have learnt: block-scoping, IIFE, carefully use of functions inside loops, MODULE, lexical scopes vs dynamics scope. I also know about so cool Revealing Module Pattern which is commonly used to build public API.
I've been writing Angular for a few years now, and I took lots of online trainings on it. I also learned a lot about TS. But never I spent any time understanding the core concepts of JS. Somehow I was receiving some clues about my ignorance on JS, but I was just keeping a blind eye on them, and continuing my learnings on more "fun" subjects, core Angular, rxjs etc etc.
Then this book appeared.
People say they don't like JS, and after reading this book, I know why!! Oh boy, I had to re-read this book twice, supplementing the ideas with more sources from the youtube (ColorCode was a good youtube channel at this point).
It is great this book is written, and it is terrible it had to be written. Now although I understand Scope and Closures, I don't like them at all, and I suspect at any job interview, the interviewer will be waiting with some "gotcha" questions on them to trap me!
Excellent coverage over these topics. One part I appreciated so much was that these topics are rarely covered well in any learning book, but are covered very well here, with reference to other ways for a more complete understanding. Kyle tells us about dynamic scoping and highlights the potential pitfalls of an area that is a mixture ("this" which in one context operates with lexical scope and in another case operates with dynamic scope). For me, the real attraction was a good understanding of closure and how it could be used. Kyle also gave a great basic understanding of closure, then gave examples that expanded the understanding into ways one might use it. The only improvement that would have helped me is practical examples in real use. But that doesn't detract from this excellent coverage. I look forward to his next book.
Did you know JavaScript is actually a compiled language? You probably did not. This and many other mind-blowing discoveries is what you will find after reading this book.
I learned what I know about JavaScript the usual way: try something; see it failing; try something else; see it working, but not really understanding why; move on with life.
This book enlightens you with the inner workings of JavaScript, specifically with something you use at every single JavaScript snippet you write, scope. It also talks in depth about closures, a usually obscure feature of JavaScript that no one seems to understand altogether.
If you, by any mean, and at any level, have to write JavaScript for your work or side-project, this series is a must.
Another must read for Javascript developers who want to understand the mechanism behind the scene. Unlike Types & Grammar which somewhat looks tricky, the knowledge in this book could really help in work. This is one of the few books that explains what is closure and why it's called closure. To simply put, a closure close a scope. But maybe most of JS developers don't even understand lexical scope much, which prevent them from understanding closures more thoroughly. And the author caught that precisely.
One nit is that if Kyle could explain more about ES6 modules, although he did great for the old ways.
I've puzzled through enough JavaScript to develop an intuitive sense of scope and closure. But the temptation is to never give it much thought in the name of churning out code that works. The reality is that "What is Closure?" is an incredibly common interview question for almost any tech screen and it pays to develop a vocabulary for discussing it.
The additional study of how to leverage an understanding of closure in one's code is invaluable. And clearly about to lead into a long discussion of "this" in the next volume.
Great book that helped me understand a little more about function and block scoping, and how to point out closures. I also got a clearer picture on when to use the variable declarations let and const. I still have a few uncertainties about using 'this' and arrow functions together, since Kyle says to avoid it and stick to one. I guess I'll know more about it in the next book in the series. : )
Overall, this book helps me think more like a JS programmer and to know that functions are powerful in and out of their scope.
This installment in YDKJS focused too much on the parts of JS that you're probably not using anymore. Yes, var has a lot of idiosyncrasies. That's why we use const now, but the book spent two sentences offhandedly mentioning const.
JavaScript has a lot of baggage from its early days, but I would argue that learning the intricacies of how people used to write JavaScript isn't very useful today. Install ESLint, enable some recommended packages, and forget that it's possible to use those old features.
This is a great book, give me a lot of new knowledge. The concept of the scope of JavaScript is quite different from another programming language. Sometimes this book is quite wordy but I think that's because of the purpose to explain for very new programmers. And I think more comments on code example as Head First Series will make this book perfect. For developers who learned or make familiar with another programming language before, I think you should remove it from your mind temporarily to understand the scope of JS.
This is a gorgeous book to understand scopes, closures, and how they are used for module patterns in JS. This is an extremely dense book and will require a few re-reads to understand it completely.
Deep and concise explanations of scopes and closures in Javascript, as well as a (useful and necessary) primer on how programming languages work in general. The book doesn't just teach you about good and bad coding practices, but helps you to develop a natural understanding of how things should be done by explaining the underpinnings of the Javascript language.
If you never quite understood hoisting or the difference between const, let and var - read this! I've never encountered a clearer explanation than this book.