👉 List of all notes for this book. IMPORTANT UPDATE Nov 18, 2024: I've stopped taking detailed notes from the book and now only highlight and annotate directly in the PDF files/book. With so many books to read, I don't have time to type everything. In the future, if I make notes while reading a book, they'll contain only the most notable points (for me).
Attention: decisions and patterns we apply across the whole program.
- How and why we use different levels of scopes (functions and blocks)? ← functions create their own scope, it’s normal but why we need blocks?
- In Software Engineering, we have The Principle of Least Privilege (POLP - a concept in computer security that limits users' access rights to only what is strictly required to do their jobs) → its variation in this section is “Least Exposure” (POLE).
- Following POLE, we want to minimize the exposure of variables in each scope.
- [Bad idea] Placing all variables in the global scope, why?
- Naming Collisions: Identical names for variables or functions can lead to collisions, resulting in bugs.
- Unexpected Behavior: If access is granted to parts that are supposed to be private, other individuals may perform actions you did not intend.
- Unintended Dependency: If developers outside of your program create a dependency on some "supposed to be private" parts of your program, future changes to these parts could unexpectedly affect these external dependencies.
→ Exposing min necessary, keeping everying else as private as possible.
- In order to block the variable to the block scope, use
letorconst, don’t usevar!
- Use
letorconstto block the scope to lowest level but how to hidevarorfunctiontoo?
- Hide
varby wrapping it by afunction.
❇️ Invoking Function Expressions Immediately
The last
() in the previous code is call Immediately Invoked Function Expression (IIFE). It’s useful to create a scropt to hide var/func.