-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBASIC - FUNCTIONS - anonymous functions.js
More file actions
158 lines (123 loc) · 3.41 KB
/
Copy pathBASIC - FUNCTIONS - anonymous functions.js
File metadata and controls
158 lines (123 loc) · 3.41 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
!!BASIC - FUNCTIONS - ANONYMOUS FUNCTIONS!!
> Functions that don’t have names
> By using anonymous functions we can often make our code less verbose,
more concise, more readable, more efficient, and even more maintainable
> Anonymous function expressions are used frequently in JavaScript code,
so if you want to be able to read other people’s code, or understand
JavaScript libraries
> Remember that JavaScript functions are always evaluated in the same scoping environment
in which they were defined.
>> Within a function, if you want to determine where a variable is coming from (local or global),
search in its enclosing functions, from the most nested to the least
*/
/*
EXAMPLE:
1) instead of this
2) use anonymous function
*/
1)
function handler() {
alert("Yeah, that page loaded!");
}
window.onload = handler;
2)
window.onload = function() {
alert("Yeah, that page loaded!");
};
/*
EXAMLE:
1) instead of this
2) use anonymous function
*/
1)
function cookieAlarm() {
alert("Time to take the cookies out of the oven");
};
setTimeout(cookieAlarm, 600000);
2)
setTimeout( function() {
alert("Time to take the cookies out of the oven");
},
600000
);
/*
NESTED FUNCTIONS
> those are functions defined within other functions
>> it affects the scope of those functions
> Functions defined at the top level of your code have global scope,
whereas functions defined within another function have local scope
RULES:
> within a function, if you define a nested function with a declaration,
that nested function is defined everywhere within the body of the function.
> if you create a nested function using a function expression, then that
nested function is defined only after the function expression is evaluated
*/
var migrating = true;
var fly = function(num) {
var sound = "Flying";
/*adding a function declaration with the name wingFlapper
inside the fly function expression*/
function wingFlapper() {
console.log(sound);
}
/*calling wingFlapper function*/
for (var i = 0; i < num; i++) {
wingFlapper();
}
};
function quack(num) {
var sound = "Quack";
/*adding a function expression assigned to the quacker
variable inside the quack function declaration*/
var quacker = function() {
console.log(sound);
};
/*calling quacker function*/
for (var i = 0; i < num; i++) {
quacker();
}
}
if (migrating) {
quack(4);
fly(4);
}
/*
CLOSURES
> A closure results when we combine a function that has free variables with
an environment that provides variable bindings for all those free variables
1) instead of this
2) use this
3) Creating a closure with an event handler
>> Closures are often used to capture state for event handlers
*/
1)
var count = 0;
function counter() {
count = count + 1;
return count;
}
2)
function makeCounter() {
var count = 0;
function counter() {
count = count + 1;
return count;
}
return counter;
}
var doCount = makeCounter();
console.log(doCount());
console.log(doCount());
console.log(doCount());
3)
window.onload = function() {
var count = 0;
var message = "You clicked me ";
var div = document.getElementById("message");
var button = document.getElementById("clickme");
button.onclick = function() {
count++;
div.innerHTML = message + count + " times!";
};
};