Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 01cbca2

Browse files
Create 14-Functions.md
1 parent f4afae8 commit 01cbca2

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

‎Notes/14-Functions.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Episode 14 : First class and Anonymous functions
2+
3+
(there is no Episode 13 idk why lol)
4+
5+
#### Function statement : Just your normal function definition
6+
7+
```
8+
function a() {
9+
console.log("a called");
10+
}
11+
a();
12+
13+
```
14+
15+
#### Function Expression : Assigning a function to a variable. Function acts like a value
16+
17+
```
18+
var b = function() {
19+
console.log("b called");
20+
}
21+
b();
22+
23+
```
24+
25+
**Difference btw function statement and expression is Hoisting**
26+
- You can put "a();" before "function a()" and it will still work. But putting "b();" before "var b = function()" throws a typeError.
27+
- Why? During mem creation phase a is created in memory and function assigned to a. But b is created like a variable (b:undefined) and until code reaches the function()
28+
part, it is still undefined. So it cannot be called.
29+
30+
#### Function Declaration : Exactly same as function statement
31+
32+
#### Anonymous Function : A function without a name
33+
- They don't have their own identity. So an anony function without code inside it results in an error.
34+
- Anony functions are used when functions are used as values eg. the code sample for function expression above
35+
36+
#### Named Function Expression : Same as Function Expression but function has a name instead of being anonymous
37+
```
38+
var b = function xyz() {
39+
console.log("b called");
40+
}
41+
b(); // prints "b called" properly
42+
xyz(); // Throws ReferenceError:xyz is not defined.
43+
44+
```
45+
> xyz function is not created in global scope. So it can't be called.
46+
47+
#### Parameters vs Arguments
48+
```
49+
var b = function(param1, param2) { // labels/identifiers that get the arg values
50+
console.log("b called");
51+
}
52+
b(arg1, arg2); // arguments - values passed inside function call
53+
54+
```
55+
#### First Class Function aka First Class Citizens
56+
- You can pass functions inside a function as arguments(WOW!)
57+
58+
```
59+
var b = function(param1) {
60+
console.log(param1); // prints " f() {} "
61+
}
62+
b(function(){
63+
64+
});
65+
66+
this can also be done:
67+
68+
var b = function(param1) {
69+
console.log(param1);
70+
}
71+
function xyz(){
72+
73+
}
74+
b(xyz); // same thing as prev code
75+
76+
you can return a function from a function:
77+
78+
var b = function(param1) {
79+
return function() {
80+
81+
}
82+
}
83+
console.log(b()); //we log the entire fun within b.
84+
85+
```
86+
#### Arrow Functions (latest in ES6 (ECMAScript 2015)
87+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /