JS is typically classified as an interpreted scripting language, so it's assumed by most that JS programs are processed in a single, top-down pass. But JS is in fact parsed/compiled in a separate phase before execution begins.
میدونستم که JS ابتدا کامپایل میشه ولی هربار که بهش فکر می کنم برام چیزای جدید داره.
Functions maintain their original scope no matter where in the program the functions are eventually executed. This is called closure.
کلوژر واقعا گیج کنندس و درکش یه کوچولو برام سخته واسه همین به همه نکاتش توجه می کنم.

تصویر خیلی کوتاه و مفید مفهوم رو میرسونه!
Scope is primarily determined during compilation, so understanding how compilation and execution relate is key in mastering scope.
همیشه سوالم بود چه فرقی می کنه حالا چه کامپایل بشه! چه تفسیر بشه...! چه فرقی داره؟!!
a program is processed by a compiler in three basic stages:
1. Tokenizing/Lexing 2. Parsing(generating AST) 3. Code Generation(generating Binary code)
کلا مبجث کامپایلر برام جالبه!!
You'll see shortly why these details about compilation and execution we do cover, even at a high level are relevant.
منتظرم بدونم چرا مهمه؟؟؟!!!
JS engines use all kinds of tricks (like JITs, which lazy compile and even hot re-compile)
processing of JS programs is that it occurs in (at least) two phases: parsing/compilation first, then execution.
برای سادگی خوبه اینجوری به قضیه نگاه کنیم.
While the JS specification does not require "compilation" explicitly, it requires behavior that is essentially only practical with a compile-then-execute approach.
There are three program characteristics you can observe to prove this to yourself: 1. syntax errors, 2. early errors, 3. and hoisting.
این دلیلهاش و مثال هایی که میاره برام تازه و جالب بودن!
In fact, the only way the JS engine could know about the syntax error on the third line, before executing the first and second lines, is by the JS engine first parsing the entire program before any of it is executed.
strict-mode forbids, among many other things, functions to have duplicate parameter names; this has always been allowed in non-strict-mode.
Could JS parse a program, but then execute that program by interpreting operations represented in the AST without first compiling the program? Yes, that is possible. But it's extremely unlikely
what the engine is doing in processing JS programs is much more alike compilation than not.
تو این بخش راجع به LHS , RHS و source refrence و target refrence حرف زده که برام جدید بود
Other than declarations, all occurrences of variables/identifiers in a program serve in one of two "roles": either they're the target of an assignment or they're the source of a value.
assignment targets and sources don't always literally appear on the left or right of an =.How do you know if a variable is a target? Check if there is a value that is being assigned to it; if so, it's a target. If not, then the variable is a source.
A function declaration is a special case of a target referenceIn case you were wondering, objects properties are not variable references.
The key idea of "lexical scope" is that it's controlled entirely by the placement of functions, blocks, and variable declarations, in relation to one another.
In other words, while scopes are identified during compilation, they're not actually created until runtime, each time a scope needs to run.
تو این بخش راجع به scope و نحوه ساخته شدنش توضیح داده... کلا نکته جدیدی برام نداشت! بنظرم سادهست.
به رفتار احمقانه جاوااسکریپت در نبود "use strict" پی بردم.
دو این بخش ۲ تا مبحث رو مطرح کرده بود:
shadowing : که بنظرم سادهست! من می فهممش !
و راجع به function expression و function declartion و arrow functoins توضیح میده و راجع به اسکوپشون بحث می کنه... . من به این نتیجه رسیدم که همیشه از function declation استفاده کنم مگر جایی که مجبور باشم.
function hoisting attachs its name identifiers to the nearest enclosing function scope (or, if none, the global scope), not a block scope.
فهمیدم function declartion بلاک اسکوپ نیست.
راجع به TDZ توضیح داده که چیز مهمیه ولی من بلدش بودم قبلا و برداشت کلیم اینه که همیشه از const استفاده کنم و اگر مجبور شدم از let .
راجع به POLE(Principle Of Least Exposure) صحبت کرده که خیلی خیلی چیز بدرد بخور و مهمیه!! من تا حالا کد خیلی پیچیدهای نزدم که که این اصل رو توش نیاز باشه رعایت کنم ولی قطعا چیز بدرد بخوریه!
در همین راستا یکی از کاربرد های IIFE رو یادگرفتم (برای ساختن یه اسکوپ میانی و cache کردن دیتا بدرد میخورن)
درباره FiB(Function decraltions in Block) کلی حرف زده:\ که نتیجهش این شد:
never place afunctiondeclaration directly inside any block. Always placefunctiondeclarations anywhere in the top-level scope of a function (or in the global scope).
FiB is not worth it, and should be avoided.
کلا راجع به کلوژر توضیح داده بود؛ از نظر من همش مهم بود واسه همین با دقت و آرامش خوندم. و بعضی نکاتی که فکر کردم برای درک و یادآوری مفهوم کلوژر لازم هست رو اینجا آوردم:
Closure is a behavior of functions and only functions
For 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.
Closure allows a function to continue to access those outer variables even after the outer scope is finished.
closure is associated with an instance of a function, rather than its single lexical definition. For each new instance, a new closure.
Closure is actually a live link, preserving access to the full variable itself. By closing over a variable in a function, we can keep using that variable (read and write).
Closure is variable-oriented not value-oriented.
If a closure exists but it cannot be observed in our programs, does it matter? No
we should look and plan for the direct, concrete effects closure has on our program behavior
Since closure is inherently tied to a function instance, its closure over a variable lasts as long as there is still a reference to that function.
Conceptually, closure is per variable rather than per scope.
if you don't need that value anymore and don't want that memory held, it's safer (memory usage) to manually discard the value rather than relying on closure optimization/GC.
کلی توضیح داده راجع به اینکه کجاها از var یا let یا const استفاده کنیم و نظرش اینه که توی اکثر جاها از var و let استفاده بشه و خیلی محدود از const استفاده بشه بهتره! جالبه که گفته استفاده از const تاثیری تو پرفورمنس نداره!
من کلا با نظرش مخالفم ولی حالا جای بحث داره! :)