-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.js
134 lines (104 loc) · 2.34 KB
/
function.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// function statement
function a(){
console.log("this is the function statmenat")
}
// console.log(a())
console.log(b)
// b() //get the error
// function expression
var b = function(name){
console.log("My Name is ",name)
}
// console.log(b("jayendra"))
// arrow function
var area = (radius)=>{
return Math.PI * radius * radius;
}
console.log(area(90));
// difference between function and arrow function
// arrow function
// not bind : this , super (instance , method)
// can't use in constructor
// can't call with new keyword
// lexical environment
function init(){
var name = "jay"
// inner function
function displayName(){
var name = "poja"
console.log(name);
}
// displayName();
console.log(name);
// return displayName;
}
// init();
// closures
// a closures is the combination of a function bundled together (enclosed) with references to its surrounding state (lexical environment)
function x(){
var a = 7 ;
function y(){
console.log(a);
}
// y();
return y;
}
// init()();
// x();
console.log(x(),typeof x)
x()();
// function z(){
// for(var i = 1; i<= 5; i++){
// setTimeout(function(){
// console.log(i);
// },i*1000);
// }
// // console.log()
// // 6
// // 6
// // 6
// // 6
// // 6
// }
// z()
// to make it closure than you can solve above the problem
// function z(){
// for(var i = 1; i<= 5; i++){
// function close(val){
// setTimeout(function(){
// console.log(val);
// },val*1000)
// }
// close(i);
// }
// }
// z()
// rest parameter
function myfun(...args){
return args.reduce((acc,e)=> acc + e);
}
console.log(myfun(2,3,5,6,21,2,3,3))
const person = {
firstName : "jayendra",
gender : "male",
age : 21
}
// parameter destructing
function printDetails({firstName,gender,age}){
console.log(firstName,gender,age)
}
printDetails(person)
// console.log()
let val1 = "this is global var"
let y = function(){
console.log(val1)
}
let fun1 = function(){
val1 = "this is block of the x"
return y;
}
//here refer noraml block scope
y()
let z = fun1(); //when after lexical bind attach than
y() //function follow the closure or its closure
// z();