Skip to content

Commit 9b53893

Browse files
author
YuChengKai
committed
# Conflicts: # Framework/framework-zh.md
2 parents 39009f7 + af7caa9 commit 9b53893

File tree

2 files changed

+225
-35
lines changed

2 files changed

+225
-35
lines changed

Algorithm/algorithm-ch.md

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
- [前驱节点](#%E5%89%8D%E9%A9%B1%E8%8A%82%E7%82%B9)
2727
- [后继节点](#%E5%90%8E%E7%BB%A7%E8%8A%82%E7%82%B9)
2828
- [树的深度](#%E6%A0%91%E7%9A%84%E6%B7%B1%E5%BA%A6)
29+
- [动态规划](#%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92)
30+
- [斐波那契数列](#%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97)
2931

3032
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
3133

JS/JS-en.md

+223-35
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
33
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
44

5-
- [Built-in Types](#built-in-types)
6-
- [Typeof](#typeof)
7-
- [New](#new)
8-
- [This](#this)
9-
- [instanceof](#instanceof)
10-
- [scope](#scope)
11-
- [Closure](#closure)
12-
- [Prototypes](#prototypes)
13-
- [inheritance](#inheritance)
14-
- [Deep and Shallow Copy](#deep-and-shallow-copy)
15-
- [shallow copy](#shallow-copy)
16-
- [deep copy](#deep-copy)
17-
- [the differences between call, apply, bind](#the-differences-between-call-apply-bind)
18-
- [simulation to implement `call` and `apply`](#simulation-to-implement---call-and--apply)
19-
- [Promise implementation](#promise-implementation)
20-
- [Throttle](#throttle)
5+
- [Built-in Types](#built-in-types)
6+
- [Typeof](#typeof)
7+
- [Type Conversion](#type-conversion)
8+
- [Converting to Boolean](#converting-to-boolean)
9+
- [Objects to Primitive Types](#objects-to-primitive-types)
10+
- [Arithmetic Operators](#arithmetic-operators)
11+
- [`==` operator](#-operator)
12+
- [Comparison Operator](#comparison-operator)
13+
- [New](#new)
14+
- [This](#this)
15+
- [Instanceof](#instanceof)
16+
- [Scope](#scope)
17+
- [Closure](#closure)
18+
- [Prototypes](#prototypes)
19+
- [Inheritance](#inheritance)
20+
- [Deep and Shallow Copy](#deep-and-shallow-copy)
21+
- [Shallow copy](#shallow-copy)
22+
- [Deep copy](#deep-copy)
23+
- [The differences between call, apply, bind](#the-differences-between-call-apply-bind)
24+
- [simulation to implement `call` and `apply`](#simulation-to-implement---call-and--apply)
25+
- [Promise implementation](#promise-implementation)
26+
- [Throttle](#throttle)
2127
- [Map、FlapMap and Reduce](#mapflapmap-and-reduce)
22-
- [async and await](#async-and-await)
28+
- [Async and await](#async-and-await)
29+
- [Proxy](#proxy)
30+
- [Why 0.1 + 0.2 != 0.3](#why-01--02--03)
31+
- [Regular Expressions](#regular-expressions)
32+
- [Metacharacters](#metacharacters)
33+
- [Flags](#flags)
34+
- [Character Shorthands](#character-shorthands)
2335

2436
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2537

26-
#### Built-in Types
38+
# Built-in Types
2739
JavaScript defines seven built-in types, which can be broken down into two categories: `Primitive Type` and `Object`.
2840

2941
There are six primitive types: `null` , `undefined` , `boolean` , `number` , `string` , `symbol `.
@@ -120,7 +132,7 @@ ToPrimitive([]) == 0
120132
1. If it's an object, `toPrimitive` is used.
121133
2. If it's a string, `unicode` character index is used to compare.
122134

123-
#### Typeof
135+
# Typeof
124136

125137
`typeof` can always display the correct type of the primitive types, except `null`
126138
```js
@@ -160,7 +172,77 @@ let undefined = 1
160172
a === void 0
161173
```
162174

163-
#### New
175+
# Type Conversion
176+
177+
## Converting to Boolean
178+
179+
Except `undefined``null``false``NaN``''``0``-0`, all of the values, including objects, are converted to `true`.
180+
181+
## Objects to Primitive Types
182+
183+
When objects are converted, `valueOf` and `toString` will be called, respectively in order. These two methods can also be overridden.
184+
185+
```js
186+
let a = {
187+
valueOf() {
188+
return 0
189+
}
190+
}
191+
```
192+
193+
## Arithmetic Operators
194+
195+
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.
196+
197+
Additions will invoke three types of type conversions: to primitive types, to numbers, and to string.
198+
199+
```js
200+
1 + '1' // '11'
201+
2 * '2' // 4
202+
[1, 2] + [2, 1] // '1,22,1'
203+
// [1, 2].toString() -> '1,2'
204+
// [2, 1].toString() -> '2,1'
205+
// '1,2' + '2,1' = '1,22,1'
206+
```
207+
208+
Note the expression `'a' + + 'b'` for addition.
209+
210+
```js
211+
'a' + + 'b' // -> "aNaN"
212+
// since ++ 'b' -> NaN
213+
// You might have seen + '1' -> 1
214+
```
215+
216+
## `==` operator
217+
218+
![](https://user-gold-cdn.xitu.io/2018/3/30/16275cb21f5b19d7?w=1630&h=1208&f=png&s=496784)
219+
220+
`toPrimitive` in the above figure is converting objects to primitive types.
221+
222+
`===` is usually recommended to compare to values. However, if you would like to know if a value is `null`, you can use `xx == null`.
223+
224+
Let's take a look at an example `[] == ![] // -> true`. The following explains why the expression evaluates to `true`.
225+
226+
```js
227+
// [] converting to true, then take the opposite to false
228+
[] == false
229+
// with #8
230+
[] == ToNumber(false)
231+
[] == 0
232+
// with #10
233+
ToPrimitive([]) == 0
234+
// [].toString() -> ''
235+
'' == 0
236+
// with #6
237+
0 == 0 // -> true
238+
```
239+
240+
## Comparison Operator
241+
242+
1. If it's an object, `toPrimitive` is used.
243+
2. If it's a string, `unicode` character index is used to compare.
244+
245+
# New
164246

165247
1. Create a new object
166248
2. Chained to prototype
@@ -226,7 +308,7 @@ new (Foo.getName());
226308
For the first function, `Foo.getName()` is executed first, so the result is 1;
227309
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.
228310

229-
#### This
311+
# This
230312

231313
`This`, a concept that many people will confuse, is not difficult to understand, as long as you remember the following rules
232314

@@ -271,7 +353,7 @@ console.log(a()()());
271353
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
272354

273355

274-
#### instanceof
356+
# Instanceof
275357

276358
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
277359
let’s try to implement it
@@ -292,7 +374,7 @@ function instanceof(left, right) {
292374
}
293375
```
294376

295-
#### scope
377+
# Scope
296378

297379
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))
298380

@@ -331,7 +413,7 @@ var b = 'Hello world'
331413

332414
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.
333415

334-
#### Closure
416+
# Closure
335417

336418
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.
337419

@@ -415,7 +497,7 @@ For `let`, it will create a block-level scope, which is equivalent to:
415497
}
416498
```
417499

418-
#### Prototypes
500+
# Prototypes
419501

420502
![](https://camo.githubusercontent.com/71cab2efcf6fb8401a2f0ef49443dd94bffc1373/68747470733a2f2f757365722d676f6c642d63646e2e786974752e696f2f323031382f332f31332f313632316538613962636230383732643f773d34383826683d35393026663d706e6726733d313531373232)
421503

@@ -426,7 +508,7 @@ Each object has an internal property, denoted as `__proto__` , which is a refere
426508
Objects can use `__proto__` to find properties that do not belong to the object, and `__proto__` connects objects together to form a prototype chain.
427509

428510

429-
#### inheritance
511+
# Inheritance
430512

431513
In ES5, we can solve the problems of inheritance by using the following ways.
432514

@@ -489,7 +571,7 @@ The Implement ideas of the above inheritance: firstly create the instance of par
489571
The inheritance implement with the above method can perfectly solve the restriction on low-level of JS.
490572
491573
492-
#### Deep and Shallow Copy
574+
# Deep and Shallow Copy
493575
494576
```js
495577
let a = {
@@ -504,7 +586,7 @@ From the above example, we can see that if you assign an object to a variable,
504586
505587
Usually, we don't want such problem to appear during development, thus we can use shallow copy to solve this problem.
506588
507-
##### shallow copy
589+
## Shallow copy
508590
509591
Firstly we can solve the problem by `Object.assign`
510592
```js
@@ -540,7 +622,7 @@ console.log(b.jobs.first) // native
540622
```
541623
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.
542624
543-
##### deep copy
625+
## Deep copy
544626
545627
The problem can usually be solved by `JSON.parse(JSON.stringify(object))`
546628
@@ -614,7 +696,7 @@ var obj = {a: 1, b: {
614696
const clone = await structuralClone(obj);
615697
```
616698
617-
#### the differences between call, apply, bind
699+
# The differences between call, apply, bind
618700
619701
Firstly, let’s tell the difference between the former two.
620702
@@ -635,7 +717,7 @@ getValue.call(a, 'yck', '24')
635717
getValue.apply(a, ['yck', '24'])
636718
```
637719
638-
##### simulation to implement `call` and `apply`
720+
## simulation to implement `call` and `apply`
639721
640722
We can consider how to implement them from the following points
641723
@@ -701,7 +783,7 @@ Function.prototype.myBind = function (context) {
701783
}
702784
```
703785
704-
#### Promise implementation
786+
# Promise implementation
705787
706788
`Promise` is a new syntax introduced by ES6, which resolves the problem of callback hell.
707789
@@ -894,7 +976,7 @@ The above codes, which is implemented based on the Promise / A+ specification,
894976
![](https://user-gold-cdn.xitu.io/2018/3/29/162715e8e37e689d?w=1164&h=636&f=png&s=300285)
895977
896978
897-
#### Throttle
979+
# Throttle
898980
899981
`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.
900982
@@ -1008,7 +1090,7 @@ function b() {
10081090
```
10091091
10101092
1011-
# async and await
1093+
# Async and await
10121094
10131095
A function with `async`, then the function will return a `Promise`.
10141096
@@ -1063,4 +1145,110 @@ You may have doubts about the above code, here explain the principle
10631145
- First the function `b` is executed. The variable `a` is still 0 before execution `await 10`, Because the `generators` are implemented inside `await` and `Generators` will keep things in the stack, so at this time `a = 0` is saved
10641146
- Because `await` is an asynchronous operation, `console.log('1', a)` will be executed first.
10651147
- At this point, the synchronization code is executed and asynchronous code is started. The saved value is used. At this time, `a = 10`
1066-
- Then comes the usual code execution
1148+
- Then comes the usual code execution
1149+
1150+
# Proxy
1151+
1152+
Proxy is a new feature since ES6. It can be used to define operations in objects.
1153+
1154+
```js
1155+
let p = new Proxy(target, handler);
1156+
// `target` represents the object of need to add the proxy
1157+
// `handler` customizes operations in the object
1158+
```
1159+
1160+
Proxy can be handly for the implementation of data binding and listening.
1161+
1162+
```js
1163+
let onWatch = (obj, setBind, getLogger) => {
1164+
let handler = {
1165+
get(target, property, receiver) {
1166+
getLogger(target, property)
1167+
return Reflect.get(target, property, receiver);
1168+
},
1169+
set(target, property, value, receiver) {
1170+
setBind(value);
1171+
return Reflect.set(target, property, value);
1172+
}
1173+
};
1174+
return new Proxy(obj, handler);
1175+
};
1176+
1177+
let obj = { a: 1 }
1178+
let value
1179+
let p = onWatch(obj, (v) => {
1180+
value = v
1181+
}, (target, property) => {
1182+
console.log(`Get '${property}' = ${target[property]}`);
1183+
})
1184+
p.a = 2 // bind `value` to `2`
1185+
p.a // -> Get 'a' = 2
1186+
```
1187+
1188+
# Why 0.1 + 0.2 != 0.3
1189+
1190+
Because JS uses the IEEE 754 double-precision version (64-bit). Every language that uses this standard has this problem.
1191+
1192+
As we know, computers uses binaries to represent decimals, so `0.1` in binary is represented as
1193+
1194+
```js
1195+
// (0011) represents cycle
1196+
0.1 = 2^-4 * 1.10011(0011)
1197+
```
1198+
1199+
How did we come to this binary number? We can try computing it as below:
1200+
1201+
![](https://user-gold-cdn.xitu.io/2018/4/26/162ffcb7fc1ca5a9?w=800&h=1300&f=png&s=83139)
1202+
1203+
Binary computations in float numbers are different from those in integers. For multiplications, only the float bits are computed, while the integer bits are used for the binaries for each bit. Then the first bit is used as the most significant bit. Therefore we get `0.1 = 2^-4 * 1.10011(0011)`.
1204+
1205+
`0.2` is similar. We just need to get rid of the first multiplcation and get `0.2 = 2^-3 * 1.10011(0011)`.
1206+
1207+
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).
1208+
1209+
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.
1210+
1211+
The native solution to this problem is shown below:
1212+
1213+
```js
1214+
parseFloat((0.1 + 0.2).toFixed(10))
1215+
```
1216+
1217+
# Regular Expressions
1218+
1219+
## Metacharacters
1220+
1221+
| Metacharacter | Effect |
1222+
| :-----------: | :----------------------------------------------------------: |
1223+
| . | Matches any character except the new line character |
1224+
| [] | matches anything within the brackets. For example, [0-9] can match any number |
1225+
| ^ | ^9 means matching anything that starts with '9'; [`^`9] means not matching characters except '9' in between brackets |
1226+
| {1, 2} | matches 1 or 2 digit characters |
1227+
| (yck) | only matches strings the same as 'yck' |
1228+
| \| | matches any character before and after \| |
1229+
| \ | escape character |
1230+
| * | Matches the preceding expression 0 or more times |
1231+
| + | Matches the preceding expression 1 or more times |
1232+
| ? | the character before '?' is optional |
1233+
1234+
## Flags
1235+
1236+
| Flag | Effect |
1237+
| :------: | :--------------: |
1238+
| i | Case-insensitive search |
1239+
| g | matches globally |
1240+
| m | multiline |
1241+
1242+
## Character Shorthands
1243+
1244+
| shorthand | Effect |
1245+
| :--: | :------------------------: |
1246+
| \w | alphanumeric characters, underline character or Chinese characters |
1247+
| \W | the opposite of the above |
1248+
| \s | any blank character |
1249+
| \S | the opposite of the above |
1250+
| \d | numbers |
1251+
| \D | the opposite of the above |
1252+
| \b | start or end of a word |
1253+
| \B | the opposite of the above |
1254+

0 commit comments

Comments
 (0)