Functions()
Functions are Objects with a special property through which we can invoke the code within them.
|
|
Return/Side Effect
Once the function hits return statement it will immediately exits from the function. If the function don’t have a return statement it will return undefined. Rest of the statements are in the function body are called the side effects. Like the console log statement.
arguments
It’s build in JS keyword which is an array like structure. It acts like an array, looks like an array but it isn’t exactly an array. It doesn’t have all the features of an array like the array METHODS but it does have the length property
So we can have different type of scenarios with arguments like if
|
|
Here the function add will have access to the variable num
because a child can access it parents variable but function foo can not have access to variable num1
. It’s not in it’s scope
Closure
Closure is when a function “remember” it’s lexical scope even when the function it executed outside that lexical scope.
Closure is a process in which the child function(function which is being returned) will have access to the variables of it parent function even though the parent function is executed and off the execution stack.
Recipe to Create Closure
1.Create a Parent Function.
2.Define some variables in the parent’s local scope.
3.Define another function inside the parent function(Child function).
4.Return the function from inside the parent function.
|
|
It’s like taking advantage of the scope. Works the same even if we are returning Objects instead of functions