-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNested Functions.js
More file actions
40 lines (31 loc) · 1.06 KB
/
Nested Functions.js
File metadata and controls
40 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* Functions can be defined inside other functions. The nested function is scoped to the outside function,
and cannot be called from the outside.
*/
//dosomethingelse() is not reachable from outside dosomething(). Attempting to reach dosomethingelse() from the
//outside will result in an error 'ReferenceError: dosomethingelse is not defined'
let dosomething = () => {
let dosomethingelse = () => {
//some code here
}
dosomethingelse()
return 'test'
}
dosomething()
/* This is very useful because we can create encapsulated code that is limited in its scope by the outer function
it’s defined in. Could have 2 function define a function with the same name, inside them. And not worry about
overwriting existing functions and variables defined inside other functions.
*/
let bark = () => {
let dosomethingelse = () => {
//some code here
}
dosomethingelse()
return 'test'
}
let sleep = () => {
let dosomethingelse = () => {
//some code here
}
dosomethingelse()
return 'test'
}