Skip to content

Commit cbffe31

Browse files
committed
docs: 算法题
1 parent 021e08e commit cbffe31

File tree

5 files changed

+97
-36
lines changed

5 files changed

+97
-36
lines changed

docs/blog/frontend.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
- es5 常规语法
2929
- es2015,2016,2017,2018 新语法
3030
- typescript 书写带类型的 js
31-
- babel 编译新 js 语法
31+
- babel / swc 编译新 js 语法
3232

3333
## 框架方向
3434

docs/blog/js-jsBridge.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# JS Bridge 总结
22

3-
构建 Native 和非 Native 间消息通信的通道,而且是 `双向通信的通道`
3+
JS Bridge 是构建 Native 和非 Native 间 `双向通信的通道`,本文主要介绍 JS Bridge 的通信策略。
4+
5+
- Js -> Native
6+
- Native -> Js
47

58
![jsBridge示意图](/blog/js-jsBridge-core.png)
69

@@ -15,7 +18,7 @@ JavaScript 调用 Native 的方式,主要有两种:注入 API 和 拦截 URL
1518
使用 Native 方式注入后,直接调用:
1619

1720
```js
18-
window.postBridgeMessage(message);
21+
window.nativeAPI.xxx(message);
1922
```
2023

2124
### 拦截 URL SCHEME
@@ -27,27 +30,26 @@ window.postBridgeMessage(message);
2730
优缺点:
2831

2932
- 兼容性好。
30-
3133
- 使用 iframe.src 发送 URL SCHEME 会有 url 长度的隐患。
3234

3335
## Native -> Js
3436

35-
Native 调用 JavaScript,其实就是执行拼接 JavaScript 字符串,从外部调用 JavaScript 中的方法,因此 JavaScript 的方法必须在全局的 window 上
37+
Native 调用 JS 比较简单,只要 H5 端将 JS 方法暴露在 Window 上给 Native 调用即可
3638

3739
## 简单实现
3840

3941
```js
4042
(function () {
41-
var id = 0,
42-
callbacks = {};
43+
var id = 0;
44+
var callbacks = {};
4345

4446
window.JSBridge = {
4547
// 调用 Native
4648
invoke: function(bridgeName, callback, data) {
4749
// 判断环境,获取不同的 nativeBridge
4850
var thisId = id ++; // 获取唯一 id
4951
callbacks[thisId] = callback; // 存储 Callback
50-
nativeBridge.postMessage({
52+
nativeAPI.postMessage({
5153
bridgeName: bridgeName,
5254
data: data || {},
5355
callbackId: thisId // 传到 Native 端
@@ -57,14 +59,11 @@ Native 调用 JavaScript,其实就是执行拼接 JavaScript 字符串,从
5759
var bridgeName = msg.bridgeName,
5860
data = msg.data || {},
5961
callbackId = msg.callbackId; // Native 将 callbackId 原封不动传回
60-
// 具体逻辑
61-
// bridgeName 和 callbackId 不会同时存在
62+
6263
if (callbackId) {
6364
if (callbacks[callbackId]) { // 找到相应句柄
6465
callbacks[callbackId](msg.data); // 执行调用
6566
}
66-
} elseif (bridgeName) {
67-
6867
}
6968
}
7069
};
@@ -73,4 +72,5 @@ Native 调用 JavaScript,其实就是执行拼接 JavaScript 字符串,从
7372

7473
### 参考链接
7574

76-
[jsBridge 原理](https://juejin.im/post/5abca877f265da238155b6bc)
75+
- [jsBridge 原理](https://juejin.im/post/5abca877f265da238155b6bc)
76+
- [](https://www.zoo.team/article/jsbridge)

docs/interview/css.md

+12
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,15 @@ touchstart -> touchmove -> touchend -> mousedown -> click -> mouseup
190190
- 允许点击,即禁止穿透(默认值):pointer-events: auto;
191191

192192
[参考资料](https://blog.csdn.net/zhuyinqinying/article/details/81775671)
193+
194+
## nth-child 和 nth-of-type 的区别
195+
196+
:nth-child(n) 选择器匹配属于其父元素的第 n 个子元素,不论元素的类型,n 可以是数字、关键词或公式。
197+
198+
:nth-of-type(n) 选择器匹配属于父元素的特定类型的第 n 个子元素的每个元素,n 可以是数字、关键词或公式。
199+
200+
![](https://img-blog.csdnimg.cn/20190221170915481.png)
201+
202+
p:nth-of-type(7) 选择的 p元素所在的父元素,下的第7个P元素 即:`<p>第7个p</p>`
203+
204+
p:nth-child(6) 选择的 p元素所在的父元素,下的第6个子元素,且该元素是P元素 即:`<p>第5个p</p>`

docs/interview/js.md

+30-23
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,25 @@ scrollWidth:
3333
- 在间隔时间内的触发,需要在间隔末尾执行一次
3434

3535
```js
36-
function throttle(callback, timeout) {
37-
var lastExecTime = 0
38-
var throttleId;
39-
return function(...rest) {
40-
var now = +new Date();
41-
var remaining = now - lastExecTime
42-
if (remaining > timeout) {
36+
function throttle(fn, timeout) {
37+
let lastExecTime = 0;
38+
let throttleId = null;
39+
return function (...rest) {
40+
const now = +new Date();
41+
const remaining = now - lastExecTime;
42+
if (remaining >= timeout) {
4343
if (throttleId) {
4444
clearTimeout(throttleId);
45+
throttleId = null;
4546
}
46-
callback.apply(this, rest);
47-
throttleId = null;
47+
fn.apply(this, rest);
4848
lastExecTime = now;
4949
} else if (!throttleId) {
5050
throttleId = setTimeout(() => {
51-
callback.apply(this, rest);
51+
fn.apply(this, rest);
52+
lastExecTime = now;
5253
throttleId = null;
53-
}, timeout-remaining);
54+
}, timeout - remaining);
5455
}
5556
};
5657
}
@@ -109,21 +110,16 @@ Array.from(arguments);
109110
- 容错处理
110111

111112
```js
112-
Function.prototype.bind2 = function(ctx, ...rest) {
113+
Function.prototype.myBind = (context, ...rest) => {
113114
if (typeof this !== 'function') {
114-
throw new Error('只能由 function 调用');
115+
throw new Error('只能作为函数调用');
115116
}
116-
const func = this;
117-
var result = function(...params) {
118-
return func.apply(
119-
// 如果是 new 对象出的,this 绑定的应该绑定为构造函数
120-
this instanceof result ? this : ctx,
121-
rest.concat(params)
122-
);
117+
const fn = this;
118+
const result = function (...params) {
119+
// 如果是 new 对象出的,this 绑定的应该绑定为构造函数
120+
return fn.apply(new.target ? this : context, [...rest, ...params]);
123121
};
124-
var fNOP = function() {};
125-
fNOP.prototype = func.prototype;
126-
result.prototype = new fNOP();
122+
result.prototype = Object.create(fn.prototype);
127123
return result;
128124
};
129125
```
@@ -408,3 +404,14 @@ new Boolean(true).valueOf(); // true
408404
- 防止窗口中的 js 操作另一个窗口中的 dom 元素,可能包含隐私信息。
409405
- 防止窗口中的 js 拿到另一个域名下的 cookie。
410406
- 保护用户隐私。
407+
408+
## 22、NodeJs 多进程怎么配置
409+
410+
需要用到 `child_process` 这个模块,主要分为 3 个方法:
411+
412+
- exec 通过回调和主进程通信,可以执行 shell 或其他命令。
413+
- spawn 通过事件流和主进程通信,可以执行 shell 或其他命令。
414+
- fork 特殊的 spawn,只能复制一个 nodejs 进程。
415+
416+
需要需要做 nodejs 端的负载均衡,可以使用 `cluster` 模块克隆多个进程。
417+

docs/interview/suanfa.md

+42
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- 贪心算法
1111
- 动态规划
1212
- 取巧方法
13+
-
1314

1415
## 查找
1516

@@ -510,3 +511,44 @@ var merge = function (intervals) {
510511
return result;
511512
};
512513
```
514+
515+
##
516+
517+
### Trie 树
518+
519+
[leetcode 208 实现 Trie 前缀树](https://leetcode-cn.com/problems/implement-trie-prefix-tree/)
520+
521+
```js
522+
class Trie {
523+
constructor() {
524+
this.root = {};
525+
}
526+
insert(word) {
527+
let temp = this.root;
528+
for (let c of word) {
529+
if (!temp[c]) {
530+
temp[c] = {};
531+
}
532+
temp = temp[c];
533+
}
534+
temp.isWord = true;
535+
}
536+
traverse(word) {
537+
let temp = this.root;
538+
for (let c of word) {
539+
if (!temp[c]) {
540+
return temp[c]
541+
}
542+
temp = temp[c];
543+
}
544+
return temp;
545+
}
546+
search(word) {
547+
const node = this.traverse(word);
548+
return !!node && !!node.isWord
549+
}
550+
startsWith(word) {
551+
return !!this.traverse(word);
552+
}
553+
}
554+
```

0 commit comments

Comments
 (0)