File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ //lexical scoping => a parent/outer function defined property has access to every fun defined and executed inside that parent/outer fun.
2+ //lexical scoping => inner fun has access to outer fun's properties.
3+ function outer ( ) {
4+ let myname = 'suraj' ;
5+ function inner ( ) {
6+ console . log ( "from inner1 " , myname )
7+ function inside_inner ( ) {
8+ console . log ( 'from inside_inner of inner1' , myname )
9+ }
10+ inside_inner ( )
11+ }
12+ let myfun = ( ) => {
13+ console . log ( "from myfun arrow ->" , myname )
14+ }
15+ inner ( )
16+ myfun ( )
17+ }
18+ outer ( )
19+
20+
21+ //closure => a function along with its lexical scope is called closure.
22+
23+ function outer2 ( ) {
24+ let username = 'piyush' ;
25+ function inner2 ( ) {
26+ console . log ( "from inner2 " , username )
27+ }
28+ return inner2
29+ }
30+ let ans = outer2 ( ) //ans is a function which has access to its lexical scope.
31+ ans ( ) //calling the inner2 function which has access to its lexical scope.
You can’t perform that action at this time.
0 commit comments