Live Note

Remain optimistic

某日被书中一段代码误导

1
2
3
4
5
6
7
8
9
10
11
var promise = new Promise(function (resolve, reject) {
resolve("ok")
setTimeout(function () {
throw new Error("test")
}, 0)
})
promise.then(function (value) {
console.log(value)
})
//ok
//Uncaught Error : test

由于前面还有这样一段话

如果 Promise 状态已经变成 Resolved,再抛出错误是无效的。

当时看到这,思索了许久,为什么已经 resolve 了这个 ERROR 还是可以往外抛出呢?
后面发现,settimeout 是一个异步函数,throw 会在下一轮事件循环开始时抛出,所以无论是否 resolve,这个 ERROR 都是会抛出的。

基本用法

1
2
3
4
5
var regex = new RegExp("xyz", "i")
var regex = new RegExp(/xyz/i)
var regex = /xyz/i

new RegExp(/abc/gi, "i").flags //ig 被覆盖成 i

match()、replace()、search()、split()

String.prototype.match 调用 RegExp.prototype[Symbol.match]
String.prototype.replace 调用 RegExp.prototype[Symbol.replace]
String.prototype.search 调用 RegExp.prototype[Symbol.search]
String.prototype.split 调用 RegExp.prototype[Symbol.split]

Read more »

又一次见证历史了。

预计级别会达到VEI6,不知道后续会不会继续喷发。
没想到,疫情还未过去,出现了一个比疫情更严峻的现实问题——无夏之年

更新于 2022 年 7 月 26 日 17:22:50

我错了,最近的温度已经到达了 40℃。

公式: $$P{Q}R\ ({P}Q{R})$$

P 和 R 都是一阶公式, 如果前提条件 P 在执行 Q 前成立, 则执行后得到满足条件 R 的状态
部分正确性断言: 如果 P 在 Q 执行前为真, 那么, 如果 Q 的执行终止,则终止在使 R 为真的某个状态
终止性断言:如果 P 在 Q 执行前为真, 那么 Q 将终止在使 R 为真的某个状态
赋值公理: $$\vdash P_0 {x:=f} P$$

推理规则的表示

$$\frac{premise -> f_0, f_1, …, f_n}{conclusion -> f_0}$$

推理规则

  • Rules of Consequence:
    $$\frac{P{Q}R,\ R\rightarrow S}{P{Q}S}\ \ \ \ \ \frac{P{Q}R,\ S\rightarrow P}{S{Q}R}$$
  • Rule of Composition:
    $$\frac{P{Q_1}R_1,\ R_1{Q_2}R}{P{Q_1, Q_2}R}$$
  • Rules of Iteration:
    $$\frac{P\ &\ B{S}P}{P\ {while\ B\ do\ S}\ \neg B \ &\ P}$$

等式公理

  • 代换: $[N/x]M$ 表示表示 M 中的自由变元 x 用 N 代换的结果, N 中的自由变元代换后不能成为约束变元
  • 约束变元改名: $\lambda x:\sigma .M = \lambda y:\sigma.[y/x]M$ 例如:$\lambda x:\sigma.x + y = \lambda z:\sigma .z+y$
  • 等价公理: 计算函数实际就是在函数中使用实在变元替换形式变元, $(\lambda x:\sigma.M)N = [N/x]M$
  • 同余性规则: 相等的函数作用于相等的变元产生相等的结果, $\frac{M_1=M_2,\ N_1=N_2}{M_1N_1=M_2N_2}$

状态模式

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
let ResultState = (() => {
let States = {
state0: function () {
console.log("state0")
},
state1: function () {
console.log("state1")
},
state2: function () {
console.log("state2")
},
state3: function () {
console.log("state3")
},
}

function show(result) {
States["state" + result] && States["state" + result]()
}

return {
show,
}
})()

// for test
ResultState.show(3)

策略模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
let Price = (() => {
let obj = {
return30: (price) => +price + parseInt(price / 100) * 30,
return50: (price) => +price + parseInt(price / 100) * 50,
percent90: (price) => (price * 100 * 90) / 10000,
percent80: (price) => (price * 100 * 80) / 10000,
}
return (algorithm, price) => {
return obj[algorithm] && obj[algorithm](price)
}
})()

// for test
let price = Price("return50", 321.13)
console.log(price)