Skip to content

Commit f142b75

Browse files
committed
interview imp- closure&lexicalscopes
1 parent c6417f5 commit f142b75

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.

0 commit comments

Comments
 (0)