Live Note

Remain optimistic

简单的 Stack 实现

数组实现

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
class Stack {
constructor(size = 10) {
this.arr = [] // 栈
this.size = size // 最大长度
this.top = -1 // 栈顶
}

push(elem) {
if (this.top == this.size) {
this.size *= 2
}
this.arr[++this.top] = elem
return true
}

pop() {
let elem = this.arr[this.top--]
return elem
}

peekTop() {
if (this.top == -1) {
console.log("stack is empty")
return
}
return this.arr[this.top]
}

print() {
let str = ""
for (let i = 0; i <= this.top; i++) {
str += this.arr[i] + " "
}
console.log(str)
}
}
var stack = new Stack()
stack.push(1)
stack.push(2)
stack.pop()
console.log(stack.peekTop())
Read more »

一个简陋的 VDOM

希望以后会慢慢完善吧…

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
let data = {
tag: "div",
children: [
{
tag: "span",
children: [
{
tag: "#text",
content: "hello world",
},
],
},
{
tag: "p",
children: [
{
tag: "#text",
content: "This is a paragraph!",
},
{
tag: "h1",
children: [
{
tag: "#text",
content: "This is a H1 tag",
},
],
},
{
tag: "h6",
children: [
{
tag: "#text",
content: "and I'm h6",
},
],
},
],
},
],
}

class VDOM {
constructor(data) {
this.tag = data.tag
this.children = data.children
this.content = data.content
}
static render(data) {
let _this = new VDOM(data)
if (_this.tag === "#text") {
return document.createTextNode(_this.content)
}
let node = document.createElement(_this.tag)
_this.children.map((child) => node.appendChild(VDOM.render(child)))
return node
}
}

let diff = (root, oldV, newV, current = 0) => {
if (!oldV) root.appendChild(newV)
else if (!newV) root.removeChild(root.childNodes[current])
else if (newV.tag !== oldV.tag || newV.content !== oldV.content)
root.replaceChild(new VDOM(newV).render(), root.childNodes[current])
// 递归判断
else
for (let i = 0; i < newV.children.length || i < oldV.children.length; i++)
diff(root.childNodes[i], oldV.children[i], newV.children[i], i)
}
document.querySelector("#root").appendChild(VDOM.render(data))

结构为:

1
2
3
4
5
6
7
8
9
10
<div id="root">
<div>
<span>hello world</span>
<p>
This is a paragraph!
<h1>This is a H1 tag</h1>
<h6>and I'm h6</h6>
</p>
</div>
</div>

《纽约时报》美国当地时间 1 月 2 日报道说,美国官员确认,在美军对巴格达的一次空袭中,伊朗伊斯兰革命卫队特种部队“圣城旅”部队指挥官卡西姆·苏莱曼尼将军死亡。

《纽约时报》报道,伊朗官方媒体确认了,伊拉克当地时间 3 日,伊朗将军卡西姆·苏莱曼尼在美军对巴格达国际机场一处目标的空袭中死亡。苏莱曼尼是伊朗革命卫队中的重要官员。美国国防部声明表示,美国发动了空袭。

“苏莱曼尼将军积极筹划了对美国在伊拉克及地区的外交官和服务人员的攻击。苏莱曼尼将军和‘圣城旅’需要为美军和联军数百人死亡、数千人受伤负起责任。”

伊朗誓言为苏莱曼尼将军之死报复美国

伊朗伊斯兰革命卫队(伊朗武装力量组成部分)前总司令、伊朗确定国家利益委员会秘书长穆赫辛·雷扎伊称,伊朗将为在巴格达死于火箭弹袭击的伊斯兰革命卫队圣城旅指挥官卡西姆·苏莱曼尼将军复仇。

雷扎伊在推特上写道:

“殉难的卡西姆·苏莱曼尼去找他的兄弟们了。我们会狠狠报复美国。”

伊斯兰革命卫队圣城旅指挥官卡西姆·苏莱曼尼将军 2 日晚上在巴格达的火箭弹袭击中被炸死,伊拉克什叶派民兵武装“人民动员组织”(Al-Hashd al-Shaabi)的几位高级别成员也一同丧生,12 名伊拉克士兵受伤。

五角大楼称系按照美国总统川普的命令开展对苏莱曼尼的行动。五角大楼称,此次打击旨在预防伊朗方面的袭击,而苏莱曼尼本人涉嫌参与袭击联盟基地和美国驻伊拉克大使馆。

子类的原型对象-类式继承

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 类式继承
// parent
function SuperClass() {
this.superValue = true
}
SuperClass.prototype.getSuperValue = function () {
return this.superValue
}

// child
function SubClass() {
this.subValue = false
}
SubClass.prototype = new SuperClass()
SubClass.prototype.getSubValue = function () {
return this.subValue
}

let child = new SubClass()
console.log(child.getSuperValue()) // true
console.log(child.getSubValue()) // false
Read more »