-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter2.js
408 lines (344 loc) · 5.42 KB
/
chapter2.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// typeof, used to examine current value and tell you what type it is
/*
a ="hello world";
console.log(typeof(a));
*/
/*
var a;
typeof a // "undefined"
a ="hello world";
typeof a //"string"
a = 42;
typeof a; // "number"
a = true;
typeof a; // "boolean"
a = null;
typeof a; // "object" - bug
a = undefined;
typeof a; // "undefined"
a = { b: "c" };
typeof a; // "object"
*/
// objects
/*
var obj = {
a: "hello world",
b: 42,
c: true
};
obj.a; // "hello world"
obj.b // "42"
obj.c // true
obj["a"]; // "hello world"
obj["b"] // 42
obj["c"] // true
*/
// Accessing different things with []
/*
var obj ={
a: "hello world",
b: 42
};
var b ="a";
obj[b]; // hello world
obj["b"] // 42
*/
// Arrays
/*
var arr= [
"hello world",
42,
true
];
arr[0]; //hello world
arr[1]; // 42
arr[2]; // true
arr.lenght; // 3
typeof arr; // object
*/
// functions
/*
function foo(){
return 42;
}
foo.bar = "hello world";
typeof foo; // "function"
typeof foo(); // "number"
typeof foo.bar // "string"
*/
// built in type methods
/*
var a = "hello world";
var b= 3.14159;
a.length; // 11
a.toUpperCase(); // "HELLO WORLD"
b.toFixed(4); // "3.1416"
*/
// comparing values
// Coercison, change the type from one to another
/*
var a = "42";
var b = Number(a);
a; // "42"
b; // 42 - the number
*/
// impicitly coerced (non obvious)
/*
var a ="42";
var b = a *1; // "42" impicitly coerced to 42 here
a; // "42"
b; // 42 the number
*/
// Truthy falsy only aplied to boolean that is coerced
/*
falsy:
"" (empty string)
0, -0, Nan (invalid number)
null, undefined
false
*/
// Equality
/*
== checks for value Equality with coercison allowed
=== checeks for value without coercio
var a = "42";
var b 42;
a == b; // true
a === b; // false
*/
// Arrays with commans sperating the number are converted to string.
/*
var a = [1,2,3];
var b = [1,2,3];
var c = "1,2,3";
a == c; // true
a == c; // true
a == b; // false
*/
// Inequality
// String values can also be compared using alphabetic rules "bar" < "foo"
/*
var a =41;
var b ="42";
var c = "43"
a < b; /// true
b < c // true
*/
/*
var a = 42;
var b = "foo";
a < b //false
a > b; // false
a == b; // false
*/
// hoisting
/*
var a = 2;
foo(); // works because or foo()
function foo() {
a = 3;
console.log ( a ); // 3
var a; // declarartion is hoisted
// to the top of foo()
}
console.log ( a ); // 2
*/
// nested scopes
/*
function foo(){
var a =1;
function bar() {
var b = 2;
function baz(){
var c =3;
console.log( a, b, c); // 123
}
baz();
console.log( a, b); // 1 2
}
bar ();
console.log( a ); // 1
}
foo ();
*/
/*
function foo(){
a = 1; // A not fomrally declared
}
// Always formally declare! dont do just a;
foo();
a; // 1 -- oopes auto globar varaible :(
*/
// using let instead of var makes it belong to only that scope
/*
function foo(){
var a =1;
if ( a >=1) {
let b = 2;
while (b < 5) {
let c = b * 2;
b++;
console.log( a + c);
}
}
}
foo();
*/
// Conditionals
/*
if (a == 2 ) {
// do something
}
else if (a == 10) {
// do another thing
}
else if (a == 42){
//do yet another thing
}
else {
// fallbak to here
}
*/
// another option with the switch statment
/*
switch (a) {
case 2:
// do something
break;
case 10:
// do something else
break;
case 42:
// do yet another thing
break;
default:
//fallback to here
}
*/
// fall through switch, if either 2 or 10 it will execute "some cool stuff"
/*
switch (a){
case 2:
case 10:
//some cool stuff
break;
case 42:
//other stuff
break;
default:
// fallback
}
*/
// conditional operator
/*
var a = 42;
var b = (a > 41) ? "hello" : "world";
*/
// similar to:
// if (a > 41) {
// b = "hello";
// }
// else {
// b = "world";
// }
// Strict mode
/*
function foo() {
"use strict";
// this code is strict mode
function bar(){
// this code is strick mode
}
}
// this code is not strict mode
*/
// Compate that to
/*
"use strict";
function foo(){
// stict mode
function bar(){
//strict mode
}
}
*/
// strict mode helps here:
/*
function foo(){
"use strict" // turn on strct mode
a = 1; // "var" missing, refrenceError
}
foo();
*/
// functions as values
// the 2n function is named bar, it's preferable to the anon one
/*
var foo = function () {
// ..
}
var x = function bar(){
//..
}
*/
// IIFE
/*
(function IIFE(){
console.log( "Hello!");
})();
// Hello
*/
//consider
/*
function foo() { .. }
// `foo` function reference expression,
// then `()` executes it
foo();
// `IIFE` function expression,
// then `()` executes it
(function IIFE(){ .. })();
*/
// scopes inside with IIFE
/*
var a = 42;
(function IIFE(){
var a = 10;
console.log( a ); // 10
})();
console.log(a); // 42
*/
// IIFE can have return values
/*
var x = (function IIFE(){
return 42;
})();
x; // 42
*/
// Closure <- Important and interesting!
/*
function makeAdder(x){
// paramter x is an inner vairable
// inner funnction add() uses x, so it has a closure over it
function add(y){
return y +x;
};
return add;
}
var plusOne = makeAdder( 1 );
var plusTen = makeAdder( 10 );
plusOne( 3 ); // 4 1 + 3
plusOne( 41 ); // 42
plusTen( 13 ); // 23
*/
// Modules
function User(){
var username, password;
function doLogin(user,pw) {
username = user;
password = pw;
// do the rest of lgoing work
}
var publicAPI = {
login: doLogin
};
return publicAPI;
}