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

惰性单例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let LazySingle = (() => {
let instance = null
function Single() {
return {
Method: function () {
console.log("public method")
},
Prototype: "some message",
}
}
return () => {
if (!instance) instance = Single()
return instance
}
})()

// for test
console.log(LazySingle().Prototype)
LazySingle().Method()

原型模式

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
let LoopImages = function (imageArr, container) {
this.imageArr = imageArr
this.container = container
}
LoopImages.prototype = {
createImage: function () {
console.log("create image")
},
changeImage: function () {
console.log("change image")
},
}

let SlideLoopImg = function (imageArr, container) {
LoopImages.call(this, imageArr, container)
}
SlideLoopImg.prototype = new LoopImages() // 继承
SlideLoopImg.prototype.changeImage = () => {
// 重写
console.log("slide loop change image")
}

// for test
let slideImage = new SlideLoopImg(["1.jpg", "2.jpg"], "slide")
console.log(slideImage.container)
slideImage.changeImage()

原型继承

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
/**
* copy object
*/
function prototypeExtend() {
let F = function () {}
// let F = new Function()

for (let i = 0; i < arguments.length; i++) {
for (let j in arguments[i]) {
F.prototype[j] = arguments[i][j]
}
}

return new F()
}

// for test
let car = prototypeExtend(
{
speed: 20,
fast: function () {
this.speed++
console.log("is faster")
},
},
{
slow: function () {
this.speed--
console.log("is slower")
},
},
{
stop: function () {
this.speed = 0
console.log("is stop")
},
},
{
currentSpeed: function () {
console.log(this.speed)
},
}
)

car.fast()
car.fast()
car.fast()
car.fast()

car.currentSpeed()
car.stop()

car.currentSpeed()

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
let Human = function (param) {
this.skill = (param && param.skill) || "null"
this.hobby = (param && param.skill) || "null"
}
Human.prototype = {
getSkill: function () {
return this.skill
},
getHobby: function () {
return this.hobby
},
}

let Named = function (name) {
let _this = this
;((name, _this) => {
this.wholeName = name
if (name.indexOf(" ") > -1) {
_this.FirstName = name.slice(0, name.indexOf(" "))
_this.LastName = name.slice(name.indexOf(" "))
}
})(name, _this)
}

let Work = function (work) {
let _this = this
;((work, _this) => {
switch (work) {
case "code":
this.work = "developer"
break
case "design":
this.work = "designer"
break
default:
this.work = work
}
})(work, _this)
}

Work.prototype.changeWork = function (work) {
this.work = work
}

/**
* to create a Person
* @param {*} name
* @param {*} work
*/
let Person = function (name, work) {
let _person = new Human()
_person.name = new Named(name)
_person.work = new Work(work)
return _person
}

// for test
let person = new Person("Jack Edward", "design")
console.log(person.work)
console.log(person.name)
person.work.changeWork("code")
console.log(person.work)

工厂模式

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
let Basketball = function () {
this.name = 'basketball'
}
Basketball.prototype = {
getMember: function () {
console.log(5)
},
getBallSize: function () {
console.log('big')
},
}

let FootBall = function () {
this.name = 'football'
}
FootBall.prototype = {
getMember: function () {
console.log(11)
},
getBallSize: function () {
console.log('big')
},
}

let Tennis = function () {
this.name = 'tennis'
}
Tennis.prototype = {
getMember: function () {
console.log(1)
},
getBallSize: function () {
console.log('small')
},
}

let SportFactory = function (name) {
switch (name) {
case 'basketball':
return new Basketball()
case 'football':
return new FootBall()
case 'tennis':
return new Tennis()
}
}

let footBall = new SportFactory('football')
footBall.getMember()
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
// Product base attribute
interface Product {
operation(): string
}

class ConcreteProduct1 implements Product {
public operation(): string {
return 'This is Product1'
}
}
class ConcreteProduct2 implements Product {
public operation(): string {
return 'This is Product2'
}
}

abstract class Creator {
public abstract factoryMethod(): Product

public operation(): string {
const product = this.factoryMethod()

return product.operation()
}
}

class ConcreteCreator1 extends Creator {
public factoryMethod(): Product {
return new ConcreteProduct1()
}
}
class ConcreteCreator2 extends Creator {
public factoryMethod(): Product {
return new ConcreteProduct2()
}
}

// usage
function clientCode(creator: Creator) {
console.log(creator.operation())
}

clientCode(new ConcreteCreator1())
clientCode(new ConcreteCreator2())

安全工厂

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
let Factory = function (type, content) {
if (this instanceof Factory) {
// check if is instance Factory
return new this[type](content)
} else {
return new Factory(type, content)
}
}
Factory.prototype = {
Java: function (content) {
console.log(content)
},
JavaScript: function (content) {
console.log(content)
},
}

let data = [
{
type: 'Java',
content: 'This is Java',
},
{
type: 'JavaScript',
content: 'This is JavaScript',
},
]

// for test
for (let i = 0; i < data.length; i++) Factory(data[i].type, data[i].content)

抽象工厂

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
/**
* to extend superType
* @param {*} subType
* @param {*} superType
*/
let VehicleFactory = (subType, superType) => {
if (typeof VehicleFactory[superType] === 'function') {
function F() {}
F.prototype = new VehicleFactory[superType]()
subType.constuctor = subType
subType.prototype = new F()
} else {
throw new Error("don' not have this abstract class")
}
}

// create base class
VehicleFactory.Car = function () {
this.type = 'car'
}
VehicleFactory.Car.prototype = {
getPrice: function () {
return new Error('abstract')
},
getSpeed: function () {
return new Error('abstract')
},
}

let BMW = function (price, speed) {
this.price = price
this.speed = speed
}
VehicleFactory(BMW, 'Car') // interface
BMW.prototype = {
getPrice: function () {
return this.price
},
getSpeed: function () {
return this.speed
},
}

// for test
let myBmw = new BMW(10000, 100)
console.log(myBmw.getPrice())
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
88
89
90
91
// product interface
interface AbstractProductA {
functionA(): string
}

interface AbstractProductB {
/**
* base operation
* @return {string}
*/
functionA(): string

/**
* inject some operation
* @param {AbstractProductA} product
* @return {string}
*/
functionB(product: AbstractProductA): string
}

// concrete production
class ConcreteProductA1 implements AbstractProductA {
functionA(): string {
return 'This is functionA from ProductA1'
}
}
class ConcreteProductA2 implements AbstractProductA {
functionA(): string {
return 'This is functionA from ProductA2'
}
}
class ConcreteProductB1 implements AbstractProductB {
functionA(): string {
return 'This is functionA from ProductB1'
}

functionB(product: AbstractProductA): string {
return 'Function B2: ' + product.functionA()
}
}
class ConcreteProductB2 implements AbstractProductB {
functionA(): string {
return 'This is functionA from ProductB2'
}

functionB(product: AbstractProductA): string {
return 'Function B2: ' + product.functionA()
}
}

// factory interface
interface AbstractFactory {
createProductA(): AbstractProductA
createProductB(): AbstractProductB
}

// concrete factory
class ConcreteFactory1 implements AbstractFactory {
createProductA(): AbstractProductA {
return new ConcreteProductA1()
}

createProductB(): AbstractProductB {
return new ConcreteProductB1()
}
}
class ConcreteFactory2 implements AbstractFactory {
createProductA(): AbstractProductA {
return new ConcreteProductA2()
}

createProductB(): AbstractProductB {
return new ConcreteProductB2()
}
}

// usage
/**
* use with different facotry.
* @param {AbstractFactory} factory
*/
function clientCode(factory: AbstractFactory) {
const productA = factory.createProductA()
const productB = factory.createProductB()

console.log(productB.functionA())
console.log(productB.functionB(productA))
}

clientCode(new ConcreteFactory1())
clientCode(new ConcreteFactory2())

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

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 »

1
Set-cookie: value[; expires=date][; domain=domain][; path=path][; secure]

每个选项都是用分号和空格来分开,每个选项都制定了 cookie 在什么情况下会发送给服务器

过期时间选项

  • expires: 指定了 cookie 最长存储时间,过期后会被浏览器删除。值是一个 date ,格式为 WDY, DD-mm-YYYY HH:MM:SS GMT
  • 没有设置 expires 选项时,默认为当前会话,所设置的 cookie 在关闭浏览器时会被删除。
1
Set-cookie: name=Jack; expires=Tue, 28 May 2019 22:33:22 GMT

domain 选项

  • domain: 指定了 cookie 将要被发给哪个域中。
  • 默认情况下会被设置为当前域。
  • 值必须是消息头主机的一部分,不合法的 domain 会直接被忽略。
1
Set-cookie: name=Jack; domain=baidu.com

path 选项

  • path: 指定了请求资源的 URL 中存在指定路径时,才会发送 cookie。
  • 只有对 domain 匹配成功后才会开始匹配 path 部分。
1
Set-cookie: name=Jack; domain=baidu.com; path=/

secure 选项

  • secure: 只是一个标记,当请求通过 SSL 或者 HTTPS 创建时,包含 secure 的 cookie 才会被发送至服务器。
  • 默认情况下, HTTPS 上传输的 cookie 都会被自动加上 secure 选项。
1
Set-cookie: name=Jack; secure

HTTPOnly 选项

  • HttpOnly: 禁止浏览器通过 JavaScript 来获取 cookie ,防止 XSS 攻击。
  • 这个属性也不能通过 JavaScript 来设置。
1
Set-cookie: name=Jack; HttpOnly
  1. 会话结束。
  2. 超过过期时间。
  3. cookie 数量达到限制,删除部分 cookie 以便为新创建的 cookie 腾出空间。

可以使用document.cookie来读取 cookie 的值。

要求

  1. 程序 1 从串口读取信息,然后发到消息队列中。使用 2 个终端运行程序 1,创建出 2 个可从串口读取数据的进程,串口可以使用 Windows 中的虚拟串口,使用串口助手输入信息。
  2. 程序 2 从消息队列中读取消息,然后在屏幕上将消息内容显示出来。

提交形式

  1. 代码
  2. 运行结果截图、串口助手截图
Read more »

TV series

  • Game of Thrones
  • WestWorld
  • Chernobyl
  • Fantasmagorias
  • Love, Death & Robots
Read more »

有名管道

  1. 父进程创建一个有名管道,然后创建 1 个子进程,父进程阻塞的方式等待有名管道的信息,当读取到信息之后,在屏幕上打印出来;当从有名管道中读取到“QUIT”之后,父进程终止。
  2. 子进程 1 使用定时器,每 5 秒钟向有名管道输入“this is process 1”;当收到信号 SIGQUIT 时,向有名管道输出“QUIT”,并在屏幕上输出“process 1 exit”之后,进程终止。

提交形式

  1. 代码
  2. 运行结果截图
Read more »