-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
202 lines (149 loc) · 4.03 KB
/
functions.js
File metadata and controls
202 lines (149 loc) · 4.03 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// FUNCTIONS
// Functions let you group a series of statements together
// to perform a specific task
/*
Functions let you group a series of statements together
to perform a specific task. If different parts
of a script repeat the same task,
You can reuse the function, rather than repeating
the same set of statements.
*/
// function Declaration
function morningGreeting(){
return 'Good morning, it is time to get up';
}
morningGreeting();//'Good morning, it is time to get up'
// another example
function greetPresident(firstName, lastName){
return 'Hello ' + firstName + ' ' + lastName + '!';
}
greetPresident('Barack', 'Obama');
// Review
// keyword function
// function name or anonymous
// code block
// called or invoked
// parameters and arguments
// practice
function goodMorningNeighbor() {
return 'Good morning, nieghbor';
}
goodMorningNeighbor()
//
function goodEveningNeighbor(){
return 'Good evening neighbor thanks for everything!';
}
goodEveningNeighbor()
//
function goodMorningNeighbor(name){
return ' Good morning, ' + name + '!';
}
goodMorningNeighbor('Codex');
//
function goodEveningNeighbor(name, task){
return 'Good morning, ' + name + ' thanks for ' + task + '!';
}
goodEveningNeighbor('Hank,', 'the million dollor investment');
// Function Expressions
// Anonymous function
// IFFES
// function expression
// This is when you assign a function as a value to a variable.
// IIFE
// Immediatley Invoked function Expressions
var greetFullName = (function(firstName, lastName) {
return 'Hello, '+ firstName + ' ' + lastName + '!';
}());
////
// practice
// function declaration must be name
function knockKnock() {
return 'Who\'s there?';
}
knockKnock();// Who's there?
// challenge
// turn knockKnock into an anonymous function within a funciton expressions
// call the function
var knockKnock = function(){
return 'Who\'s there?';
}
// value of knockKnock you get function
/* ƒ (){
return 'Who\'s there?';
}
*/
// but when you invoke the function or call the function
knockKnock(); // Who's there?
// practice
// Immediatley Invoked function Expressions
function dogWalker(person, dog) {
return person + ' is taking ' + dog + ' for a walk.';
}
dogWalker('Paul', 'Charlie');
"Paul is taking Charlie for a walk."
// challenge
// create an immediately invoked function expression
// store the IIFE a variable and call it
(function(person, dog){
return person + 'is taking ' + dog + ' for a walk.';
}('Paul', 'Charlie'));
var dogWalker = (function(person, dog){
return person + 'is taking ' + dog + ' for a walk.';
}('Paul', 'Charlie'));
// Scope
// function scope
// scope is where a variable is defined.
// Javascript has two scopes global and local
var scope = 'public'; // Global
function checkScope(){
var scope = 'private'; // local
return scope;
}
// review funciton scope
// scope determined by where variable is defined
// global and local
// scope chain
// 'use strict' mode
//practice
function roadTrip() {
var gallons = 12; // bad practice to use var inside local scope
var mpg = 34; // bad practice to use var inside local scope
return gallons * mpg;
}
roadTrip(); // 408
// challenge
// scope chain
// local scope => parent scope => grandparent scope=> great grandparent scope =>
// etc... => global scope => error
// Best practice
var gallons = 12;
var mpg = 34;
function roadTrip() {
return gallons * mpg;
}
roadTrip(); // 408
// strict mode example in the console with an IFFE
(function(){
'use strick';
food = 'pizza';
}());
// Nested function
// build a nested function
// child scope => parent scope => global scope
// run the results of the child scope
// have the innermost child function grab a value from the parent and
// grandparent scope
// global scope
var height = 10;
function volume() {
// parent scope
var width = 10;
var length = 10;
var volumeOfObject = function(){
// child or local scope
return length * width * height;
}
// return value of function expression volumeOfObject
return volumeOfObject()
}
volume()//1000