-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudyJS.js
1646 lines (1407 loc) · 48.8 KB
/
studyJS.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict'; // strict mode, must use var
//////////////////////////////////////
// decorator
var oldLog = console.log
var countLog = 0
console.log = function() {
countLog++;
return oldLog.apply(null, arguments);
}
var x = 1;
x = 0xff00;
console.log(Number.MAX_SAFE_INTEGER);
var s = false === 0;
s = (false == 0) + (false === 0);
var bi1 = 9223372036854775807n; // bigInt
var bi2 = BigInt(1235);
s = bi1 / bi2;
// s = bi1 / 123; exception
//alert('我要学JavaScript!' + s);
// null和undefined. Python is None
// 大多数情况下,我们都应该用null。undefined仅仅在判断函数参数是否传递的情况下有用
x = [0,1,2,"test", null, true]
console.log(x);
x = new Array(1,2,3.14)
console.log(x);
var person = {
name : 'Snake',
age : 20,
tags : ['js', 'web', 'mobile'],
code : null
}
console.log(person);
console.log(person.tags)
console.log(person.code)
// var person = null; good
//console.log(person);
//////////////// string //////////
var s = 'I\'m "snake".\n'
s += '\x41'; // 完全等同于 'A'
s += '\u4e2d\u6587'; // 完全等同于 '中文'
console.log(s)
s =
`line 1
line 2`;
console.log(s)
var addr = 'beijing';
s = `hello, ${person.name}, age is ${person.age}, live in ${addr}. `;
console.log(s)
console.log(s.length)
// s[0]='A' // exception error. string is readonly
console.log(s.indexOf('age') + ' ' + s.indexOf('notFound'))
console.log(s.substring(1,5))
///////////////////// array ////////////////
console.log('//////////////// array ///////////////////////')
var a = [1,2,3];
a.length = 6; // add undefined
console.log(a)
a[10] = '10' // add undefined
a[5]=1
a[6]=2
console.log(a)
console.log(a.indexOf(1))
console.log(a.slice(1,5))
a.pop()
a.push(11)
console.log(a)
a.shift()
a.unshift('a','b')
console.log(a)
a.sort()
console.log(a)
var a = [11,12,1,2,3,44];
a.sort() // [1, 11, 12, 2, 3, 44]
console.log(a)
var a = ['Microsoft', 'Apple', 'Yahoo', 'AOL', 'Excite', 'Oracle'];
console.log(a)
a.splice(2,3,'Google', 'Facebook') // 从索引2开始删除3个元素,然后再添加两个元素:
console.log(a)
var b = ['A', 'B', 'C'];
console.log(a.concat(b))
console.log(a + b)
console.log("a is not changed: " + a) // a is not changed
console.log(a.join('--'))
var a = [[1, 2, 3], [400, 500, 600], '-'];
console.log(a[1].indexOf(500))
/////////////////////////
var person = {
name : 'Snake',
age : 20,
tags : ['js', 'web', 'mobile'],
code : null
}
console.log(person)
console.log(person.new)
person.new = 'new'
console.log(person.new)
console.log('name' in person)
console.log('Name' in person) // case sensitive
console.log('toString' in person)
console.log(person.hasOwnProperty('toString'))
console.log('notExist' in person)
/////////////////////////////////
if (person.age >= 18)
console.log('adult: ' + person.age)
else if (person.age >= 6)
console.log('teenager: ' + person.age)
else
console.log('kid: ' + person.age)
// JavaScript把null、undefined、0、NaN和空字符串''视为false,
// 其他值一概视为true,因此上述代码条件判断的结果是true。
var s = '123'
if (s.length)
console.log('true')
else
console.log('false')
var r = 0
for (var i = 1; i <= 10;i++) {
r += i
}
console.log(r)
var o = person
for (var key in o) {
if (o.hasOwnProperty(key)) {
console.log(key + ' : ' + o[key])
}
else {
// why: output nothing?
console.log(key + ' () : ' + o[key])
}
}
var a = [0, 1,2,'a','b']
for (var i in a) {
console.log(typeof(i) + ' : ' + i)
console.log(typeof(a[i]) + ' : ' + a[i])
}
//////////////// map and set //////////////
var m = new Map([['mike', 95], ['apple', 92], ['bob', 91]]);
var s = new Set();
console.log('///////////////// map and set ///////////////')
console.log('support map and set!')
console.log(m.get('mike'))
console.log(m.get('bob'))
console.log(m.has('adam'))
m.set('adam', 67)
console.log(m.has('adam'))
console.log(m.get('adam'))
m.delete('notexist') // not report error
m.delete('adam')
console.log(m.get('adam')) // undefined
// add a property named wrongKey
// NOT: add the value into map
m['wrongKey'] = "this is value for wrong key"
console.log(`m.has('wrongKey'): ` + m.has('wrongKey')) // return false
console.log(m['wrongKey']) //
console.log(m['notExist']) // undefined
var m = new Object()
console.log('test: add property to object')
m[1] = "this is 1 property" // add a property named 1
console.log(m[1]) //
console.log(m[0]) // undefined
var s = new Set([1,2,3,3,3,'3','2']);
console.log(s.size)
console.log(s.has('3'))
s.add(4)
s.delete('123')
console.log(s.size)
console.log(s)
//////////////////////////////////
// 为了统一集合类型,ES6标准引入了新的iterable类型,Array、Map和Set都属于iterable类型。
console.log('///////////////// iterable ///////////////')
var m = new Map([['mike', 95], ['apple', 92], ['bob', 91]]);
for (var key in m) { // undefined
// NOT reach here
console.log(key + ' : ' + m.get(key))
}
for (var v of m) {
console.log(v)
console.log(v[0] + ' : ' + v[1])
}
var s = new Set([1,2,3,3,3,'3','2']);
for (var v of s) {
console.log(typeof(v))
console.log(v)
}
var a = [0,1,2]
console.log('test array iterator')
for (var v in a) {
console.log(v)
}
for (var v of a) {
console.log(v)
}
a.name = "name"
for (var v in a) {
console.log(v) // wrong: name is included
}
for (var v of a) {
console.log(v) // correct: name is NOT included
}
console.log('test array forEach')
a.forEach(function(e,i,a) {
console.log(e + ', index = ' + i);
console.log(a.length);
}
);
console.log('test map forEach')
m.forEach(function(value, key, m) {
console.log(value + ', key = ' + key);
// console.log(a.size);
}
);
console.log('test set forEach')
s.forEach(function(e,sameElement,s) {
console.log(e + ', SameElement = ' + sameElement);
// console.log(typeof(e))
// console.log(s.size);
}
);
///////////////// function /////////////////
console.log('///////////////// function ///////////////')
var abs = function(x) {
if (typeof(x) != 'number')
console.log('No a Number')
return null
console.log(typeof(arguments)) // object
console.log('arguments.length: ' + arguments.length) // object
for (var v of arguments) {
console.log('arg: ' + v)
}
if (x >= 0)
return x;
else
return -x;
};
var x = 'abc'
var s = typeof(x)
console.log(typeof(s)) // typeof return a string
//console.log(abs('abc')) // exception
console.log(abs(-2))
console.log(abs(2))
console.log(abs()) // return NaN
function foo(a,b, ...rest) { // rest argument
console.log(typeof(arguments)) // object
console.log('a: ' + a)
console.log('b: ' + b)
console.log(b != undefined)
console.log('foo() arguments.length: ' + arguments.length) // object
for (var v of arguments) {
console.log('arg: ' + v)
}
console.log('rest: ' + rest)
return null
}
console.log(foo(1))
console.log(foo(1, 2))
console.log(foo(1, 2,3,4,5))
function foo1()
{
var x = 'hello: ' + y;
y = x;
console.log(x)
// y is moved to top of the function; but value is undefined
var y = 'bob'
console.log(y)
}
console.log(foo1()) //
// window is the default global object
var course = 'course'
console.log(window)
console.log('window.name: ' + window.name)
console.log(course)
console.log(window.course) // course is bound to window's property
var oldAlert = window.alert;
//oldAlert('use old alert') // good alert
window.alert = function() {}
alert('NOT function') // NOT work
window.alert('NOT function') // NOT work
window.alert = oldAlert
//alert('function again') // work again
// use one unique global variable
var my = {};
my.name = 'snake'
my.version = '1.0.0'
my.foo = function(arg) {
return 'my.foo(): ' + arg
}
console.log(my.foo(my.name))
function testDomain() {
var r = 0
for (var i = 0; i < 3; i++)
r += i
console.log(i) // still able to access i
// use let for block variable
{
let i =0
for (i = 0; i < 10; i++)
r += i
// this is the 'let i'
console.log(i)
}
// this is the 'var i'
console.log(i)
/////////////////////////
// const
const pi = 3.14
// pi = 3.1415
console.log('pi: ' + pi) // exception TypeError
}
testDomain()
function testAssign () {
// 'undefined'
console.log(x);
console.log(y);
console.log(z);
var x,y,z;
x,y,z = 1,2,3
// 'undefined, undefined, 1'
console.log(`${x}, ${y}, ${z}`);
// TypeError: Cannot set properties of undefined (setting '1')
// [x,y,z] = [1,2,3]
([x,y,z] = [1,2,3]);
// '1, 2, 3'
console.log('[x,y,z]: ' + `${x}, ${y}, ${z}`)
var person = {
name : 'Snake',
age : 20,
tags : ['js', 'web', 'mobile'],
address : {
city : 'beijing',
street:'No.1',
zipcode:'525000'
},
code : null
}
var {name,age,tags, address:{city}} = person
// Uncaught ReferenceError: address is not defined
// console.log(`${name}, ${age}, ${tags}, ${address}`)
console.log(`${name}, ${age}, ${tags}, ${city}`);
({name,age} = {name:'test',age:20} );
// Uncaught ReferenceError: address is not defined
// console.log(`${name}, ${age}, ${tags}, ${address}`)
console.log(`${name}, ${age}, ${tags}, ${city}`)
var {name,age,tags, age:ageName, age:ageName2} = person
// age is NOT variable; it is to help ageName variable to get the value
console.log(`${name}, ${age}, ${tags}, ${ageName}, ${ageName2}`)
var {name,age,tags, notExist = 'default value'} = person
// age is NOT variable; it is to help ageName variable to get the value
console.log(`${name}, ${age}, ${tags}, ${notExist}`)
// swap value
x = 100;
y = 200;
[x,y] = [y,x]
console.log('x,y: ' + x + ', ' + y)
var {hostname: host, pathname: path} = location;
console.log(`host: ${host}, path: ${path}`);
}
testAssign()
function buildDate({year, month, day, hour = 0, minute = 0, second = 0}) {
return new Date(`${year}-${month}-${day} ${hour}:${minute}:${second}`)
}
console.log(buildDate({year:2023, month : 1, day : 27}));
console.log(buildDate({year:2023, month : 1, day : 27, hour : 6}));
function buildDate2(year, month, day, hour = 0, minute = 0, second = 0) {
return new Date(`${year}-${month}-${day} ${hour}:${minute}:${second}`)
}
console.log(buildDate2(2023, 1, 27));
console.log(buildDate2(2023, 1, 27,6));
///////////////////////////////////////////////
console.log('//////////////// object\'s method ///////////////////////')
var person = {
name : 'Snake',
age : 20,
tags : ['js', 'web', 'mobile'],
address : {
city : 'beijing',
street:'No.1',
zipcode:'525000'
},
code : null
}
var Bob = {
name : 'bob',
age : 20,
birth : 1995
}
person.birth = 1990;
person.age = function() {
var ret = new Date().getFullYear();
console.log('this: ' + this)
if (this === undefined) {
return undefined
}
return ret - this.birth;
}
console.log('age: ' + person.age())
// 'this' is the global window.this, which is undefined
var functionGetAge = person.age;
console.log('age: ' + functionGetAge())
// test: apply()
function getAge() {
var ret = new Date().getFullYear();
console.log('this: ' + this)
return ret - this.birth;
}
person.age = getAge
console.log('age: ' + person.age())
console.log('apply: ' + getAge.apply(person, []))
console.log('call: ' + getAge.call(Bob))
console.log('call max(): ' + Math.max.call(null, 1,3,5))
console.log('call max(): ' + Math.max.call(Bob, 1,3,5))
//////////////////////////////////////
// decorator
// oldLog is console.log
var m = Math.max
console.log(m(1,2,3))
console.log(Math.max(1))
console.log(m.apply(null, [1,2,3,4,5]))
oldLog("old log 1")
oldLog("old log 2")
console.log("1")
console.log("2")
console.log('count log(): ' + countLog)
/////////////////////////////////////////////////////
console.log(`///////////// high-order functions //////////////////`)
function add(x, y, f) {
return f(x) + f(y);
}
console.log("add(x,y,f): " + add(5,-6,Math.abs))
console.log("add(x,y,f): " + add(5,-6,Math.pow))
console.log(`///////////// map and reduce //////////////////`)
function pow(x) {
return x * x;
}
var arr = [1,3,5,7,9]
var results = arr.map(pow)
console.log("arr.map(pow): " + results)
var results = arr.map(String)
console.log("arr.map(String): " + results)
var results = arr.map(Math.pow)
console.log("arr.map(Math.pow): " + results) // why: [1, 2, 9, 64, 625]
function mul(x,y) {
return x * y;
}
console.log("arr.reduce(mul): " + arr.reduce(mul));
console.log("arr.reduce(10*x+y): " + arr.reduce(function(x,y) {
return 10 * x + y;
}));
console.log(`///////////// string2int(s) //////////////////`)
function string2int(s) {
// var arr = s.split()
var arr = Array.from(s);
// console.log("size: " + arr.length + " " + arr)
var results = arr.reduce(function(x,y , index) {
// console.log(typeof(x) + " " + typeof(y))
console.log(`string2int: [${x}] [${y}] [${index}]`)
return 10 * (x - '0') + (y - '0');
}, '0'); // provide default parameter if length is 1
// console.log("typeof(results): " + typeof(results))
return results;
}
console.log(string2int('34567'));
console.log(string2int('7'));
console.log(string2int('0'));
console.log(string2int('12300'));
function normalize3(arr) {
var results = arr.map(function(ele, index) {
var tempArr = Array.from(ele);
tempArr = tempArr.map(function(x,index) {
if (index == 0) {
return x.toUpperCase();
}
return x.toLowerCase();
});
// tempArr[0] = tempArr[0].toUpperCase();
return tempArr.join('')
});
return results;
}
function normalize2(arr) {
var results = arr.map(function(ele, index) {
// console.log(`ele: ${ele} index: ${index}`)
var tempArr = Array.from(ele);
tempArr = tempArr.map(function(x) {
return x.toLowerCase();
// console.log('not reach')
// if (x > 'Z') {
// return x;
// }
// else {
// return String.fromCharCode('a'.charCodeAt() + (x.charCodeAt() - 'A'.charCodeAt()))
// }
});
// console.log("tempArr: " + typeof(tempArr))
// console.log("tempArr: " + tempArr.length)
// console.log("tempArr: " + tempArr)
// console.log("tempArr: " + tempArr.join(''))
tempArr[0] = tempArr[0].toUpperCase();
// console.log("tempArr: " + tempArr.join(''))
return tempArr.join('')
});
// console.log("typeof(results): " + typeof(results))
// console.log("results.length: " + results.length)
return results;
}
function normalize(arr) {
var results = arr.map(function(ele, index) {
return ele[0].toUpperCase() + ele.substring(1).toLowerCase();
});
return results;
}
var r = normalize(['adam', 'LISA', 'barT'])
console.log(r)
console.log(r.toString())
console.log(`normalize: ${r}`)
console.log(`///////////// filter //////////////////`)
var arr = [1,2,4,5,9,10,13,14,15];
var r = arr.filter(function(x) {
return (x % 2 == 1)
});
console.log(r);
var arr = ['A', '', 'B', null, undefined, 'C', ' '];
var r = arr.filter(function(x){
return x && x.trim() != '';
});
console.log(r);
arr = ['apple', 'strawberry', 'banana', 'pear', 'apple', 'orange', 'orange', 'strawberry'];
var r = arr.filter(function(ele, index, self){
return self.indexOf(ele) == index;
});
console.log(r);
var arr = [];
for (var i = 0; i < 100; i++) {
arr[i] = i
}
function isPrime(x) {
if (x < 2) {
return false;
}
for (var i = 2; i <= Math.sqrt(x); i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
function getPrime(arr) {
return arr.filter(function(x) {
return isPrime(x);
});
}
var r = getPrime(arr);
console.log(r);
console.log(`///////////// sort //////////////////`)
console.log("sort: default by ASCII; modify the array")
var arr = ['Google', 'Apple', 'Microsoft'];
console.log(arr.sort());
var arr = ['Google', 'apple', 'Microsoft'];
console.log(arr.sort());
var arr = [10, 20, 1, 2]
console.log(arr.sort() + " // sort by ASCII");
function increSort(x,y) {
if (x == y)
return 0
return x > y ? 1 : -1;
}
console.log("add self-soft: " + arr.sort(increSort))
var r = arr.sort(function(x,y) {return -increSort(x,y);});
console.log("add self-soft: " + r);
function increSortString(x,y) {
if (x === y)
return 0;
return x.toLowerCase() > y.toLowerCase() ? 1 : -1;
}
var arr = ['Google', 'apple', 'Microsoft'];
console.log("add self-soft: " + arr.sort(increSortString))
console.log("add self-soft: " + arr) // sort: modify the array
console.log(`///////////// Array: other methods //////////////////`)
console.log("sort: default by ASCII")
var arr = ['Google', 'apple', 'Microsoft'];
console.log(arr.every(function(x){
return x.length >= 6;
}));
console.log(arr.every(function(x){
return x.length >= 3;
}));
function findLower(x) {
return x.toLowerCase() == x;
}
function findUpper(x) {
return x.toUpperCase() == x;
}
console.log("findLower(): " + arr.find(findLower));
console.log("findUpper(): " + arr.find(findUpper));
console.log("findLower() index: " + arr.findIndex(findLower));
console.log("findUpper() index: " + arr.findIndex(findUpper));
function outputLog(x) {
console.log(x)
}
var arr = ['Google', 'apple', 'Microsoft'];
console.log("forEach: default 3 parameters (element, index, array)");
arr.forEach(console.log)
console.log("forEach: 1 parameter (element)");
arr.forEach(outputLog)
console.log(`arr.forEach((u,i) => {console.log(u,i)})`)
arr.forEach((u,i) => {console.log(u,i)})
console.log(`arr.forEach(function(u,i){console.log(u,i)})`)
arr.forEach(function(u,i){console.log(u,i)})
console.log('////////////////// closure: return high-order function as a result //////////////////////')
function sum(arr) {
return arr.reduce(function(x, y) {
return x + y;
})
}
console.log("sum(): " + sum([1,2,3,4,5]))
function lazySum(arr) {
return function() {
return arr.reduce(function(x, y) {
return x + y;
})
}
}
var f = lazySum([1,2,3,4,5])
console.log("f(): " + f())
function count() {
var arr = [];
for (var i =1; i <= 3; i++) {
arr.push(function() {
return i * i;
});
}
return arr;
}
var functions = count();
var f0 = functions[0];
var f1 = functions[1];
var f2 = functions[2];
console.log(f0())
console.log(f1())
console.log(f2())
function count2() {
var arr = [];
for (var i =1; i <= 3; i++) {
var iObj = new Object();
iObj.i = i;
arr.push(function() {
return iObj.i * iObj.i;
});
}
return arr;
}
var functions = count2();
var f0 = functions[0];
var f1 = functions[1];
var f2 = functions[2];
console.log(f0())
console.log(f1())
console.log(f2())
console.log("create an anonymous function and execute it to return the internal function")
function count3() {
var arr = [];
for (var i =1; i <= 3; i++) {
arr.push(
function(x) {
return function() {
return x * x;
}
}(i)
);
}
return arr;
}
var functions = count3();
var f0 = functions[0];
var f1 = functions[1];
var f2 = functions[2];
console.log(f0())
console.log(f1())
console.log(f2())
console.log("closure: encapsulate a private variable")
function createCounter(init) {
var x = init || 0;
return { // key : value
inc : function() {
x++;
return x;
}
}
}
var c1 = createCounter();
console.log(c1.inc());
console.log(c1.inc());
console.log(c1.inc());
var c2 = createCounter(100);
console.log(c2.inc());
console.log(c2.inc());
console.log(c2.inc());
function makePow(n) {
return function(x) {
return Math.pow(x, n)
}
}
var pow2 = makePow(2);
var pow3 = makePow(3);
console.log(pow2(5));
console.log(pow3(5));
console.log("closure: define calculation operation")
var zero = function(f) {
return function(x) {
return x;
}
}
var one = function(f) {
return function(x) {
// console.log("f.name: " + f.name)
return f(x)
}
}
function add(n, m) {
return function addReturn(f) {
return function(x) {
console.log(`add(n,m): ${x}, ${f.name}, ${m.name}, ${n.name}`);
return m(f)(n(f)(x));
}
}
}
function testOutput(x) {
console.log("testOutput: " + x);
return;
}
function testOutputWithReturn(x) {
console.log("testOutputWithReturn: " + x);
return "return of testOutputWithReturn: " + x;
}
console.log("closure: test zero")
zero(testOutput)();
console.log("/////////////////// closure: test one")
console.log(one);
console.log(one(testOutput));
console.log(one(testOutput)());
console.log(one(testOutput)(111));
console.log(one(testOutputWithReturn)());
console.log(one(testOutputWithReturn)(111));
console.log("/////////////////// closure: test two")
var two = add(one, one)
console.log("console.log(two(testOutput)());")
console.log(two(testOutput)());
console.log("console.log(two(testOutput)(222));")
console.log(two(testOutput)(222));
console.log("console.log(two(testOutputWithReturn)(222));")
console.log(two(testOutputWithReturn)(222));
console.log("/////////////////// closure: test five")
var five = zero;
for (var i = 0; i < 5; i++) {
five = add(five, one);
}
five(testOutput)();
five(testOutputWithReturn)(555);
console.log("closure: test two + one")
var three = add(add(one, one), one);
three(testOutput)()
three(testOutputWithReturn)()
three(testOutputWithReturn)(333)
console.log("closure: test one + two")
var three = add(one, add(one, one));
three(testOutput)()
three(testOutputWithReturn)()
three(testOutputWithReturn)(333)
console.log("closure: testFunction")
var testFunction = function(f) {
return f()
}
console.log(testFunction)
console.log(testFunction(testOutput))
//console.log(testFunction(testOutput)()) // TypeError
//console.log(testFunction(testOutput)("xxx"))
console.log('////////////////// arrow function //////////////////////')
var f = function(x) {
return x * x;
}
console.log(f(5))
var f = x => x * x;
console.log(f(5))
var f = () => 3.14;
console.log(f(5))
// use brackets for multiple parameters
var f = (x, y) => x * x + y * y;
console.log(f(5, 6))
var f = (x, ...rest) => {
var i, sum = x;
for (i = 0; i < rest.length; i++) {
sum += rest[i]
}
return sum;
}
console.log(f(5, 6, 7, 8, 9))
// use brackets when return object
var f = x => ({ foo: x})
console.log(f(5))
/*
箭头函数看上去是匿名函数的一种简写,
但实际上,箭头函数和匿名函数有个明显的区别:
箭头函数内部的this是词法作用域,由上下文确定
*/
console.log("arrow function: different this")
// wrong example
var obj = {
birth : 1990,
getAge: function() {
// 'this' is bound to correct obj
var b = this.birth;
console.log("this.birth: " + this.birth)
var fn = function() {
// 'this' is bound to window or undefined
// runtime error: Uncaught TypeError: Cannot read properties of undefined
console.log("this.birth: " + this.birth)
return new Date().getFullYear() - this.birth;
}
return fn();
}
}
// will cause error
// console.log("obj.getAge(): " + obj.getAge())
// correct example
var obj = {
birth : 1990,
getAge: function() {
// 'this' is bound to correct obj
var b = this.birth;
console.log("this.birth: " + this.birth)
// use arrow function: 'this' is bound to window or undefined
var fn = () => new Date().getFullYear() - this.birth;
return fn();
}
}
// correct
console.log("obj.getAge(): " + obj.getAge())
console.log("use arrow function to simplify parameters for sort()")
var arr = [10, 20, 1, 2]
arr.sort((x, y) => {
// if (x === y)
// return 0;
return x > y ? 1 : -1;
})
console.log(arr);
console.log('////////////////// Tagged Template Literals //////////////////////')
const email = '[email protected]'
const password = 'hello123'
var sqlstrings = undefined
function sql(strings, ...exps){
sqlstrings = strings
console.log(`strings: ${strings}`)
console.log(strings)
console.log(`SQL: ${strings.join('???')}`)