-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddle.html
679 lines (514 loc) · 14.4 KB
/
middle.html
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
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
//***语法*****************************************
function foo(x) {
return listener
}
var evt = foo(42)
evt.on('completion', function() {
})
evt.on('failure', function(err) {
})
// function add(xPromise, yPromise) {
// return Promise.all([xPromise, yPromise]).then(function(values) {
// return values[0] + values[1]
// })
// }
// add(fetchX(), fetchY()).then(function(sum) {
// console.log(sum)
// }, function(err) {
// console.error(err)
// })
// function add(getX, getY, cb) {
// var x, y
// getX(function(xVal) {
// x = xVal
// if (y != undefined) {
// cb(x + y)
// }
// })
// getY(function(yVal) {
// y = yVal
// if (x != undefined) {
// cb(x + y)
// }
// })
// }
// add(fetchX, fetchY, function(sum) {
// console.log(sum)
// })
// function asyncify(fn) {
// var origFn = fn
// var intv = setTimeout(function() {
// intv = null
// if (fn)
// fn()
// }, 0)
// fn = null
// return function() {
// if (intv) {
// fn = origFn.bind.apply(
// origFn,
// [this].concat([].slice.call(arguments))
// )
// } else {
// origFn.apply(this, arguments)
// }
// }
// }
// function result(data) {
// console.log(a)
// }
// var a = 0
// ajax('../pre/url', asyncify(result))
// a ++;
/*
代码块:
[] + {}; // [object Object]
{} + []; // 0
先执行 && 再执行||,最后是? :
*/
// var a = 42
// var b = 'foo'
// var c = false
// var d = a && b || c ? c || b ? a : c && b : a // 42
// function foo() {
// try {
// return 42
// }
// finally {
// console.log('Hello')
// }
// console.log('never runs.')
// }
// console.log(foo())
// foo: for (var i = 0; i < 4; i ++) {
// for (var j = 0; j < 4; j ++) {
// if (j == i) {
// continue foo
// }
// if ((j * i) % 2 == 1) {
// continue
// }
// console.log(i, j)
// }
// }
// console.log('**************')
// bar: for (var i = 0; i < 4; i ++) {
// for (var j = 0; j < 4; j ++) {
// if ((i * j) >= 3) {
// console.log('stopping!', i, j)
// break bar
// }
// console.log(i, j)
// }
// }
// function vowels(str) {
// var matches
// if (str && (matches = str.match(/[aeiou]/g))) {
// return matches
// }
// }
// console.log(vowels('Hello world...'))
//***强制类型转换**********************************
// var a = 1.07 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000
// console.log(a.toString())
// var o = {}
// var b = {
// c: 42,
// d: o,
// d: function() {}
// }
// o.e = b
// console.log(JSON.stringify(o))
// b.toJSON = function() {
// return { c: this.c }
// }
// console.log(JSON.stringify(b))
// JSON.stringify
// var a = {
// b: 42,
// c: '42',
// d: [1, 2, 3]
// }
// console.log('JSON.stringify(a, [b, c])', JSON.stringify(a, ['b', 'c']))
// var helperFn = JSON.stringify(a, function(k, v) {
// if (k !== 'c') {
// return v
// }
// }, '****')
// console.log('helperFn', helperFn)
/*
ToNumber:
1.调用valueOf
2.调用toString
ToBoolean:
1.假值:undefined null false +0 -0 NaN ''
2.假值对象
var a = new Boolean(false) // true
var b = New Number(0) // true
var c = new String('') // true
3.真值
a + '' 和String(a)的区别:
1.a + '':先调用valueOf,在调用ToString
2.String(a):直接调用ToString
*/
// var a = {
// valueOf: function() { return 42 },
// toString: function() { return 4 }
// }
// var a = 42
// var b = '42'
// // 因为没有强制类型转换,所以a === b为false
// console.log(a === b)
// console.log(a == b)
// var a = '42'
// var b = true
// console.log(a == b)
// var x = '42'
// var y = false
// console.log(x == y)
// Number.prototype.valueOf = function() {
// return 3
// }
// console.log(new Number(2) == 3)
// var i = 2
// Number.prototype.valueOf = function() {
// return i ++
// }
// var a = new Number(42)
// if (a == 2 && a == 3) {
// console.log('Hppened.....')
// }
/*
x : number
y : string
x == ToNumber(y)
x: string
y: number
ToNumber(x) == y
x: boolean
ToNumber(x) == y
y: boolean
x == ToNumber(y)
x: string/number
y: object
x == ToPrimitive(y)
x: object
y: string/number
ToPrimitive(x) == y
*/
// 一些比较
// console.log('0' == null)
// console.log('0' == undefined)
// console.log('0' == false)
// console.log('0' == NaN)
// console.log('0' == 0)
// console.log('0' == '')
// console.log(false == null)
// console.log(false == undefined)
// console.log(false == NaN)
// console.log(false == 0)
// console.log(false == '')
// console.log(false == [])
// console.log(false == {})
// console.log('' == null)
// console.log('' == undefined)
// console.log('' == NaN)
// console.log('' == 0)
// console.log('' == []) //
// console.log('' == {})
// var a = { b: 42 }
// var b = { b: 43 }
// console.log(a < b)
// console.log(a == b)
// console.log(a > b)
// console.log(a <= b) // true
// console.log(a >= b) // true
// function onlyOne() {
// var sum = 0
// for (var i = 0; i < arguments.length; i ++) {
// if (arguments[i]) {
// sum += arguments[i]
// }
// }
// return sum === 1
// }
// console.log(+new Date())
// // polyfill : Date.now()
// if (!Date.now) {
// Date.now = function() {
// return +new Date()
// }
// }
// console.log(parseInt(1/0, 19))
// console.log(parseInt(0.0008))
// console.log(parseInt(0.000000008)) // 8e-9
// console.log(parseInt(false, 16))
// console.log(parseInt(parseInt, 16))
// console.log(parseInt('0x10'))
// console.log(parseInt('103', 2))
// var a = [1, 2]
// var b = [3, 4]
// console.log(a + b)
// var a = {
// valueOf: function() {
// return '42'
// }
// }
// var b = {
// toString: function() {
// return '42'
// }
// }
// var c = [4, 2]
// c.toString = function() {
// return this.join('')
// }
// console.log(Number(a))
// console.log(Number(b))
// console.log(Number(c))
// console.log(Number(''))
// console.log(Number([]))
// console.log(Number(['a, b, c']))
//***原生函数*************************************
// 通过构造函数(如new String("abc"))创建出来的是封装了基本类型值(如"abc")的封装对象。
// var a = new String('abc')
// console.log(typeof a) // object
// console.log(a instanceof String) // true
// console.log(Object.prototype.toString.call(a)) // [object String]
// var a = new Boolean(false)
// // 我们为false创建了一个封装对象,然而该对象是真值
// if (!a) {
// console.log('boolean')
// }
// 如果想要自行封装基本类型值,可以使用Object(..)函数
// var a = 'abc'
// var b = new String(a)
// var c = Object(a)
// console.log(typeof a)
// console.log(typeof b)
// console.log(typeof c)
// console.log(b instanceof String)
// console.log(c instanceof String)
// console.log(Object.prototype.toString.call(b))
// console.log(Object.prototype.toString.call(c))
// var a = new String('abc')
// var b = new Number(42)
// var c = new Boolean(true)
// console.log(a.valueOf())
// console.log(b.valueOf())
// console.log(c.valueOf())
// // 第二个参数则必须是一个数组(或者类似数组的值,也叫作类数组对象,array-like object)
// var d = Array.apply(null, {length: 3})
// console.log(a) // [undefined, undefined, undefined]
// var c = new Object()
// c.foo = 'bar'
// var d = {foo: 'bar'}
// var e = new Function('a', 'return a * 2')
// var f = function(a) { return a * 2 }
// function g(a) { return a* 2 }
// var h = new RegExp('^a*b+', 'g')
// var i = /^a*b+/g;
/*
1.RegExp(..)有时还是很有用的,比如动态定义正则表达式时 new RegExp("pattern","flags")
2.不要把Function(..)当作eval(..)的替代品,你基本上不会通过这种方式来定义函数。
3.a.valueOf()
4.Date.now()
5.Symbol
*/
// var name = 'Kyle'
// var namePattern = new RegExp('\\b(?:)' + name + '\\b', 'ig')
// var matches = someText.match(namePattern)
// function foo(x) {
// if (!x) {
// throw new Error('x not provided')
// }
// }
// var mysym = Symbol('my own symbol')
// console.log('mysym', mysym)
// console.log(typeof mysym)
// var a = {}
// a[mysym] = 'foobar'
// console.log('Object.getOwnPropertySymbols(a)', Object.getOwnPropertySymbols(a))
// function isThisCool(vals, fn, rx) {
// vals = vals || Array.prototype;
// fn = fn || Function.prototype;
// rx = rx || RegExp.prototype;
// return rx.test(vals.map(fn).join(''))
// }
// isThisCool(); // true
// isThisCool(['a', 'b', 'c'], function(v) { return v.toUpperCase();}, /D/); // false
//***类型*****************************************
/*
七大内置类型
null
undefined
boolean
number
string
object 除对象之外,其他统称为“基本类型”
symbol
*/
// console.log(typeof null) // object
// var a = null
// console.log(!a && typeof a === 'object') // true
/*
1.正确的返回结果应该是"null",但这个bug由来已久,在JavaScript中已经存在了将近二十年,也许永远也不会修复
2.null是“假值” 也是唯一一个用typeof检测会返回"object"的基本类型值。
*/
// typeof function a() {} // function
// function doSomethingCool() {
// var helper = (typeof FeatureXYZ !== 'undefined' ?
// FeatureXYZ:
// function() {})
// var val = helper()
// }
// 数组
/*
类数组转数组:
1.Array.prototype.slice.call(arguments)
2.Array.from(arguments)
*/
// function foo() {
// var arr = Array.prototype.slice.call(arguments)
// arr.push('bam')
// console.log(arr)
// }
// foo('a', 'b')
// 字符串
// var a = 'foo'
// var b = ['f', 'o', 'o']
// console.log(a.concat('bar'))
// var c = Array.prototype.join.call(a, '-')
// var d = Array.prototype.map.call(a, function(v) {
// return v.toUpperCase() + '.'
// }).join('')
// console.log('c', c)
// console.log('d', d)
// // reverse
// var e = a.split('').reverse().join('')
// console.log('e', e)
// // 数字
// var f = 5E10
// console.log('f', f.toExponential())
// // 不过对于.运算符需要给予特别注意,因为它是一个有效的数字字符,会被优先识别为数字字面量的一部分,然后才是对象属性访问运算符。
// var g = 42.59
// console.log(g.toFixed(0))
// console.log(g.toFixed(2));
// // 42.toFixed(3) // 无效
// (32).toFixed(0) // 有效
// 0.32.toFixed(0) // 有效
// 32..toFixed(0) // 有效
// 32 .toFixed(9) // 有效
// // 精度问题 - 机器精度
// if (!Number.EPSILON) { // ES6支持
// Number.EPSILON = Math.pow(2, -52)
// }
// function numbersCloseEnoughToEqual(n1, n2) {
// return Math.abs(n1 - n2) < Number.EPSILON
// }
// var a = 0.1 + 0.2
// var b = 0.3
// console.log(numbersCloseEnoughToEqual(a, b))
// console.log(numbersCloseEnoughToEqual(0.0000001, 0.0000002))
/*
Number.MAXVALUE
Number.MINVALUE
Number.MAXSAFEINTEGER
整数的检测
Number.isInteger()
Number.isSafeInteger()
*/
// console.log(Number.isInteger(343.000)) // true
// console.log(Number.isInteger(343.003)) // false
// // polyfill
// if (!Number.isInteger) {
// Number.isInteger = function(num) {
// return typeof num == 'number' && num % 1 == 0
// }
// }
// if (!Number.isSafeInteger) {
// Number.isSafeInteger = function(num) {
// return Number.isInteger(num) && Math.abs(num) <= Number.MAXSAFEINTEGER
// }
// }
/*
undefined类型只有一个值,即undefined。
null类型也只有一个值,即null。
它们的名称既是类型也是值。
undefined
1.永远不要重新定义undefined。
void
1.void______没有返回值,因此返回结果是undefined
*/
/*
NaN
1.执行数学运算没有成功,这是失败后返回的结果”
2.不要使用window.isNaN,用Number.isNaN
3.NaN是JavaScript中唯一一个不等于自身的值。
*/
// window.isNaN('foo') // true !!!!居然是true
// // polyfill Number.isNaN
// if (!Number.isNaN) {
// // Number.isNaN = function(n) {
// // return typeof n === 'number' && window.isNaN(n)
// // }
// Number.isNaN = function(n) {
// // NaN是JavaScript中唯一一个不等于自身的值。
// return n !== n
// }
// }
// Number.isNaN('foo') // false
// // 负零
// /*
// 作用:有些应用程序中的数据需要以级数形式来表示(比如动画帧的移动速度),数字的符号位(sign)用来代表其他信息(比如移动的方向)。此时如果一个值为0的变量失去了它的符号位,它的方向信息就会丢失。所以保留0值的符号位可以防止这类情况发生。
// */
// function isNegZero(n) {
// n = Number(n)
// return (n === 0) && (1 / n === -Infinity)
// }
// console.log(isNegZero(-0))
// console.log(isNegZero(0 / -3))
// console.log(isNegZero(0))
// // Object.is() 判断两个值是否绝对相等
// console.log(Object.is(2 / 'foo', NaN))
// console.log(Object.is(-0, 0))
// console.log(Object.is(-0, -0))
// // polyfill
// if (!Object.is) {
// Object.is = function(v1, v2) {
// if (v1 === v2 && v2 === 0) { // 判断-0
// return 1 / v1 === 1/ v2
// }
// if (v1 !== v1) { // 判断NaN
// return v2 !== v2
// }
// return v1 === v2 // 其他
// }
// }
// 值
// 引用
// function foo(x) {
// x.push(4)
// console.log(x)
// // x = [4, 5, 6]
// // x.push(7)
// // console.log(x)
// x.length = 0
// x.push(4, 5,6, 7)
// console.log(x)
// console.log(x.length)
// }
// var a = [1, 2, 3]
// foo(a)
// console.log(a) // [1, 2, 3, 4]
//*************************************************
</script>
</body>
</html>