Live Note

Remain optimistic

教育部关于同意复旦大学章程部分条款修改的批复 教政法函〔2019〕18 号

重点修改如下:

序言第一段

“国人自主创办”改为“中国人自主创办”。

序言第二段

删去“学校的办学理念是其校歌所传颂的……思想自由……强调以学术精神滋养师生,坚守价值,治学严谨,为学有恒。学校始终秉持团结、服务与牺牲的精神,强调学校、师生的社会责任和国家使命。”增加“敦行爱国奉献……海纳百川、追求卓越。学校倡导‘文明、健康、团结、奋发’的校风和‘刻苦、严谨、求实、创新’的学风,强调坚持理想、探究真理、正谊明道、守护文明。”

Read more »

codePointAt()

JavaScript-16 的格式储存,每个字符固定为两个字节。需要四个字节储存的字符,JavaScript 会认为那是两个字符。
ES6 提供了 codePointAt 方法,能够正确处理四个字节储存的字符,返回一个字符的码点。

String.fromCodePoint()

识别 UTF-32 字符。

includes()、startsWith()、endsWith()

1
2
3
4
5
6
7
var s = "hello world"
s.startWith("hello") //true
s.endsWith("d") //true
s.includes("o") //true

s.startsWith("world", 6) //true;
s.includes("hello", 6) //false

repeat()

1
2
3
4
5
6
7
8
9
10
11
"x".repeat(3) //'xxx'
"na".repeat(0) //''

"na".repeat(2.9) //取整 -> 'nana'

"na".repeat(Infinity) //Error
"na".repeat(-1) //Error

"na".repeat(-0.9) //''
"na".repeat(NaN) //''
"na".repeat("3") //'nanana'
Read more »

属性名表达式

JavaScript 定义属性有两种方法:

1
2
obj.foo = true
obj["a" + "bc"] = 123

ES6 允许字面量定义对象时使用第二种方法:

1
2
3
4
5
6
7
8
let propKey = "foo"
let obj = {
[propKey]: true,
["a" + "bc"]: 123,
["he" + "llo"]() {
return "hi"
},
}
Read more »

尾调用

Tail Call 是函数式编程的一个重要概念,就是指某个函数的最后一步是调用另一个函数。

1
2
3
function f(x) {
return g(x)
}

尾调用不一定出现在函数尾部,只要是最后一步操作即可。

1
2
3
4
function f(x) {
if (x > 0) return t(x)
return m(x)
}

尾调用优化

函数调用会在内存中形成一个 call frame,保存调用位置和内部变量等信息。所有的 call frame 形成一个 call stack。
尾调用由于是函数的最后一步操作,所以不需要保留外层函数的 call frame,这就叫 Tail Call Optimization,即只保留内层函数的调用帧。

尾递归

递归非常耗内存,因为需要同时保存多个 call frame,很容易发生 stack overflow。但对于尾递归来说,由于只存在一个 call frame,所以不会发生溢出。

1
2
3
4
5
6
7
function Fibonacci(n) {
if (n <= 1) return 1
return Fibonacci(n - 1) + Fibonacci(n - 2)
}
console.log(Fibonacci(10)) //89
console.log(Fibonacci(100)) //overflow
console.log(Fibonacci(500)) //overflow
Read more »

工厂模式

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())