Live Note

Remain optimistic

装饰器

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
/**
* 为dom元素添加事件
* @param {} dom
* @param {*} type
* @param {*} fn
*/
function addEvent(dom, type, fn) {
if (dom.addEventListener) {
dom.addEventListener(type, fn, false)
} else if (dom.attachEvent) {
dom.attachEvent("on" + type, fn)
} else {
dom["on" + type] = fn
}
}

/**
* 装饰已有对象
* @param {*} id
* @param {*} fn
*/
function decorator(id, fn) {
let obj = document.querySelector(id)
if (typeof id.onclick === "function") {
let oldFn = id.onclick
id.onclick = function () {
oldFn()
fn()
}
} else {
obj.onclick = fn
}
}

观察者模式

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
let Observer = (() => {
let _message = {}
return {
regist: function (type, fn) {
if (typeof _message[type] === "undefined") {
_message[type] = [fn]
} else {
_message[type].push(fn)
}
},
fire: function (type, args) {
if (!_message[type]) {
return
}
let events = {
type,
args: args || {},
}
for (let i = 0, len = _message[type].length; i < len; i++) {
_message[type][i].call(this, events)
}
},
remove: function (type, fn) {
if (_message[type] instanceof Array) {
for (let i = _message[type].length - 1; i >= 0; i--) {
_message[type][i] === fn && _message[type].splice(i, 1)
}
}
},
}
})()

Observer.regist("test", function (e) {
// register
console.log(e.type, e.args.msg)
})
Observer.fire("test", { msg: "this is some test message" }) // send

  • background: 背景的可爱萌妹子, 当然可以自定义背景
  • beautify: 代码格式化
  • Dracula Official: 颜色主题
  • Haskell Syntax Highlighting: Haskell 语法高亮
  • HTML Snippets: HTML 自动补全
  • HTML CSS Support
  • JavaScript (ES6) code snippets: JS 自动补全
  • Markdown PDF: 神器, 但是默认会装一个 Chromium
  • npm Intellisense: 自动导入模块
  • Path Intellisense: 自动补全文件名
  • Quokka.js: 方便 debug
  • Vetur
  • vscode-icons: vscode 文件图标
  • Vue 2 Snippets
  • yddict(npm): 查词, 非常方便, 安装:sudo npm i yddict -g, 用法: yd hello
  • http-server(npm)

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class Node {
constructor(data = -1, prev = null, next = null) {
this.data = data
this.prev = prev // 向前指针
this.next = next // 向后指针
}
}

class LinkList {
constructor() {
this.head = this.tail = null // 首、尾指针
this.size = 0 // 元素个数
}

addHead(elem) {
let elemNode = new Node(elem)
if (this.size == 0) {
this.head = this.tail = elemNode
this.size++
} else {
this.head.prev = elemNode
elemNode.next = this.head
this.head = elemNode
this.size++
}
return true
}

deleteHead() {
if (this.size != 0) {
let elem = this.head.data
this.head.prev = null
this.head = this.head.next
this.size--
return elem
} else {
console.log("list is empty")
return
}
}

addTail(elem) {
let elemNode = new Node(elem)
if (this.size == 0) {
this.head = this.tail = elemNode
this.size++
} else {
elemNode.prev = this.tail
this.tail.next = elemNode
this.tail = elemNode
this.size++
}
return true
}

deleteTail() {
if (this.size != 0) {
let elem = this.tail.data
this.tail.next = null
this.tail = this.tail.prev
this.size--
return elem
} else {
console.log("list is empty")
return
}
}

display() {
let current = this.head,
count = this.size,
str = ""
while (count > 0) {
str += current.data + " "
current = current.next
count--
}
console.log(str)
}
}
let linklist = new LinkList()
linklist.addHead(1)
linklist.addHead(2)
linklist.addHead(3)
linklist.deleteHead()
linklist.addTail(4)
linklist.display()

结果

2 1 4

基本用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let [a, b, c] = [1, 2, 3]
let [foo, [[bar], baz]] = [1, [[2], 3]]

let [, , third] = ["foo", "bar", "baz"]
third //'baz'
let [x, , y] = [1, 2, 3]
x, y //1, 3

let [head, ...tail] = [1, 2, 3, 4]
head //1
tail //[2, 3, 4]

let [x, y, ...z] = ["a"]
x //a
y //undefined
z //[]

let [x, y, z] = new Set(["a", "b", "c"])
x //'a'
Read more »

简单的 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 »