functions(){}

Functions()

Functions are Objects with a special property through which we can invoke the code within them.

1
2
3
4
5
6
7
8
9
function foo(a , b) {
// function Declaration. 'a' and 'b' are called the parameters passed to the function. Parameters are like placeholders. These are variable that don't have a value until we assign a value in the arguments.
console.log(a);
return a + b;
// function body. This is what will be executed when we invoke the function. Function body never runs unless until it is called.
}
foo(12 , 12); // 24
//Function call/Invoking the function with '()' parenthesis and what passed through these are called arguments. Even if we have 2 parameters we can have multiple arguments because JS is really flexible and it doesn't mind the extra arguments. It will just ignore the extra ones.

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

=== 4``` then do this or `else if` ```arguments === 5``` then do that
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
### Constructor
Constructor is a function that returns an `Object`.
### Scope
It defines where we can access a variable. Where to look for a variable.
To declare a global variable inside a function
1. leave the `var` and just declare the variable it will be global
2. We can attach it to the window object as well. window.__
A child function can access the variable of it's parent functions but a parent can not access the child's variables. A function can go outside to look for the variable but it can't go inside another functions to look for it.
```javascript
function foo(num) {
return function add(num1) {
return num + num1
}
}
foo(12)(12); // 24

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.

1
2
3
4
5
6
7
8
var innerVar = "Global Scope";
function parent() {
var innerVar = "Local Scope";
function child() {
return innverVar;
}
return child;
}

It’s like taking advantage of the scope. Works the same even if we are returning Objects instead of functions

Proudly powered by Hexo and Theme by Hacker
© 2017 Mohit Garg