You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
37
37
38
-
####Built-in Types
38
+
# Built-in Types
39
39
JavaScript defines seven built-in types, which can be broken down into two categories: `Primitive Type` and `Object`.
40
40
41
41
There are six primitive types: `null` , `undefined` , `boolean` , `number` , `string` , `symbol `.
@@ -63,7 +63,7 @@ console.log(a) // EF
63
63
```
64
64
65
65
66
-
####Typeof
66
+
# Typeof
67
67
68
68
`typeof` can always display the correct type of the primitive types, except `null`
69
69
```js
@@ -103,27 +103,27 @@ let undefined = 1
103
103
a ===void0
104
104
```
105
105
106
-
####Type Conversion
106
+
# Type Conversion
107
107
108
-
#####Converting to Boolean
108
+
## Converting to Boolean
109
109
110
-
Other than`undefined`, `null`, `false`, `NaN`, `''`, `0`, `-0`, all of the values, including objects, are converted to `true`.
110
+
Except`undefined`, `null`, `false`, `NaN`, `''`, `0`, `-0`, all of the values, including objects, are converted to `true`.
111
111
112
-
#####Objects to Primitive Types
112
+
## Objects to Primitive Types
113
113
114
114
When objects are converted, `valueOf` and `toString` will be called, respectively in order. These two methods can also be overridden.
115
115
116
116
```js
117
117
let a = {
118
118
valueOf() {
119
-
return0
119
+
return0
120
120
}
121
121
}
122
122
```
123
123
124
-
#####Arithmetic Operators
124
+
## Arithmetic Operators
125
125
126
-
Only for additions, if one of the parameters is a string, the other will be converted to string as well. For all other operations, as long as one of the parameters is a number, the other will be converted to a number.
126
+
Only for additions, if one of the parameters is a string, the other will be converted to the string as well. For all other operations, as long as one of the parameters is a number, the other will be converted to a number.
127
127
128
128
Additions will invoke three types of type conversions: to primitive types, to numbers, and to string.
129
129
@@ -144,13 +144,13 @@ Note the expression `'a' + + 'b'` for addition.
`toPrimitive` in the above figure is converting objects to primitive types.
152
152
153
-
`===` is usually recommeded to compare to values. However, if you would like to know if a value is `null`, you can use `xx == null`.
153
+
`===` is usually recommended to compare to values. However, if you would like to know if a value is `null`, you can use `xx == null`.
154
154
155
155
Let's take a look at an example `[] == ![] // -> true`. The following explains why the expression evaluates to `true`.
156
156
@@ -168,13 +168,12 @@ ToPrimitive([]) == 0
168
168
0==0// -> true
169
169
```
170
170
171
-
#####Comparison Operator
171
+
## Comparison Operator
172
172
173
173
1. If it's an object, `toPrimitive` is used.
174
174
2. If it's a string, `unicode` character index is used to compare.
175
-
3.
176
175
177
-
####New
176
+
# New
178
177
179
178
1. Create a new object
180
179
2. Chained to prototype
@@ -240,7 +239,7 @@ new (Foo.getName());
240
239
For the first function, `Foo.getName()` is executed first, so the result is 1;
241
240
As for the latter, it firstly executes `new Foo()` to create an instance, then finds the `getName` function on `Foo` via the prototype chain, so the result is 2.
242
241
243
-
####This
242
+
# This
244
243
245
244
`This`, a concept that many people will confuse, is not difficult to understand, as long as you remember the following rules
246
245
@@ -285,7 +284,7 @@ console.log(a()()());
285
284
Actually , the arrow function does not have `this` , `this` in the above function only depends on the first function outside that is not arrow function . In above case , `this` is default to `window` because calling `a` matches the first situation in the above codes . And , what `this` is bound to will not be changed by any code once `this` is bound to the context
286
285
287
286
288
-
#### instanceof
287
+
#Instanceof
289
288
290
289
The `instanceof` operator can correctly judge the type of the object , bacause it’s internal mechanism is to find out if `prototype` of this type can be found in the prototype chain of the object
291
290
let’s try to implement it
@@ -306,7 +305,7 @@ function instanceof(left, right) {
306
305
}
307
306
```
308
307
309
-
#### scope
308
+
#Scope
310
309
311
310
Executing JS code would generate execution environment , as long as the code is not written in a function , it belongs to the global execution environment . The code in a function will generate function execution environments , but only two (there’s an `eval`, which basically will not be used, so you can think of only two execution environments))
312
311
@@ -345,7 +344,7 @@ var b = 'Hello world'
345
344
346
345
Using `var` is more likely to make mistake , thus ES6 introduces a new keyword `let` . `let` has an important feature that it can’t be used before declared , which mismatches the often saying that `let` doesn’t have the ability of hoisting . Indeed, `let` hoists declared , but does not assign a value, because the temporary dead zone.
347
346
348
-
####Closure
347
+
# Closure
349
348
350
349
The definition of a closure is simple: Function A returns a function B, and function B uses a variable of function A, and the function B is called a closure.
351
350
@@ -429,7 +428,7 @@ For `let`, it will create a block-level scope, which is equivalent to:
The shallow copy only solves the problem of the first layer. If the object contains objects, then it returns to the beginning topic that the values of both share the same reference. To solve the problem, we need to introduce deep copy.
556
555
557
-
##### deep copy
556
+
## Deep copy
558
557
559
558
The problem can usually be solved by `JSON.parse(JSON.stringify(object))`
560
559
@@ -628,7 +627,7 @@ var obj = {a: 1, b: {
628
627
constclone=awaitstructuralClone(obj);
629
628
```
630
629
631
-
#### the differences between call, apply, bind
630
+
# The differences between call, apply, bind
632
631
633
632
Firstly, let’s tell the difference between the former two.
634
633
@@ -649,7 +648,7 @@ getValue.call(a, 'yck', '24')
649
648
getValue.apply(a, ['yck', '24'])
650
649
```
651
650
652
-
##### simulation to implement `call` and `apply`
651
+
## simulation to implement `call` and `apply`
653
652
654
653
We can consider how to implement them from the following points
655
654
@@ -715,7 +714,7 @@ Function.prototype.myBind = function (context) {
715
714
}
716
715
```
717
716
718
-
#### Promise implementation
717
+
# Promise implementation
719
718
720
719
`Promise` is a new syntax introduced by ES6, which resolves the problem of callback hell.
721
720
@@ -908,7 +907,7 @@ The above codes, which is implemented based on the Promise / A+ specification,
`Debounce` and `Throttle` are different in nature. `Debounce` is to turn multiple executions into the last execution, and `Throttle` is to turn multiple executions into execution at regular intervals.
The effect of the `Map` is to generate a new array, iterate over the original array, take each element out to do some transformation, and then `append` to the new array.
977
976
@@ -1022,7 +1021,7 @@ function b() {
1022
1021
```
1023
1022
1024
1023
1025
-
#### async and await
1024
+
# Async and await
1026
1025
1027
1026
A function with `async`, then the function will return a `Promise`.
1028
1027
@@ -1079,13 +1078,13 @@ You may have doubts about the above code, here explain the principle
1079
1078
- At this point, the synchronization code is executed and asynchronous code is started. The saved value is used. At this time, `a =10`
1080
1079
- Then comes the usual code execution
1081
1080
1082
-
#### Proxy
1081
+
# Proxy
1083
1082
1084
1083
Proxy is a new feature since ES6. It can be used to define operations in objects.
1085
1084
1086
1085
```js
1087
1086
let p =newProxy(target, handler);
1088
-
// `target` represents the object to add the proxy to
1087
+
// `target` represents the object of need to add the proxy
Because JS incorporated IEEE 754 for double float numbers (64 bits). Every language that uses this standard has this problem.
1121
+
Because JS uses the IEEE 754 double-precision version (64-bit). Every language that uses this standard has this problem.
1123
1122
1124
-
As you know, computers uses binaries to represent decimals, so `0.1` in binary is represented as
1123
+
As we know, computers uses binaries to represent decimals, so `0.1` in binary is represented as
1125
1124
1126
1125
```js
1127
1126
// (0011) represents cycle
@@ -1138,40 +1137,40 @@ Binary computations in float numbers are different from those in integers. For m
1138
1137
1139
1138
Back to the double float for IEEE 754 standard. Among the 64 bits, one bit is used for signing, 11 used for integer bits, and the rest 52 bits are floats. Since `0.1` and `0.2` are infinitely cycling binaries, the last bit of the floats needs to indicate whether to round (same as rounding in decimals).
1140
1139
1141
-
After rounding, `2^-4*1.10011...001` becomes `2^-4*1.10011(0011*12次)010`. After adding these two binaries we get `2^-2*1.0011(0011*11次)0100`, which is `0.30000000000000004` in decimals.
1140
+
After rounding, `2^-4*1.10011...001` becomes `2^-4*1.10011(0011*12 times)010`. After adding these two binaries we get `2^-2*1.0011(0011*11 times)0100`, which is `0.30000000000000004` in decimals.
1142
1141
1143
1142
The native solution to this problem is shown below:
0 commit comments