Fork me on GitHub

js函数式编程-读书笔记

最近,工作不是很忙,赶紧为自己充电。准备很长一段事件撸函数式编程
如有不正确的地方,请大家提出来,我会更正,共同进步,谢谢。

基于流的编程 or 无类编程

连接

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
function createPerson () {
let firstName = '';
let lastName = '';
let age = 0;
return {
setFirstName: function (fn) {
firstName = fn;
return this;
},
setLastName: function (fn) {
lastName = fn;
return this;
},
setAge: function (fn) {
age = fn;
return this;
},
toString: function () {
return [firstName, lastName, age].join(' ');
}
};
}
let dd = createPerson()
.setFirstName('yu')
.setLastName('fan')
.setAge(11)
.toString();
console.log(dd) // yu fan 11

管道

pipeline(a, _.compact, _.initial, _.rest, rev) // 对数据a进行一系列的操作

数据流与控制流

无类编程

数据导向

Mixins

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
类层次结构
function ContainerClass () {}
function ObservedContainerClass () {}
function HoleClass () {}
function CASClass () {}
function TableBaseClass () {}

ObservedContainerClass.prototype = new ContainerClass();
HoleClass.prototype = new ObservedContainerClass();
CASClass.prototype = new HoleClass();
TableBaseClass.prototype = new HoleClass();

// 用Mixin扁平化层级结构
function Container (val) {
this._value = val;
this.init(val);
}
Container.prototype.init = _.identity;
let Hole = function (val) {
Container.call(this, val);
}
let HoleMixin = {
setValue: function (newValue) {
let oldVal = this._value;
this.validate(newValue);
this._value = newValue;
this.notify(oldVal, newValue);
return this._value;
},
};
let ObserverMixin = (function () {
let _watchers = [];
return {
watch: function (fun) {
_watchers.push(fun);
return _.size(_watchers);
},
notify: function (oldVal, newVal) {
_.each(_._watchers, function (watcher) {
watcher.call(this, oldVal, newVal);
});
return _.size(_watchers);
}
};
});
let ValidateMixin = {
addValidator: function (fun) {
this._validator = fun;
},
init: function (val) {
this.validate(val);
},
validate: function (val) {
if (existy(this._validator) &&
!this._validator(val)) {
fail('Attrmpted to set invalid value' + polyToString(val));
}

}
};
_.extend(Hole.prototype, HoleMixin, ValidateMixin, ObserverMixin); // 同级继承
let h = new Hole(43);
h.addValidator(always(false));
console.log(h); // 43

let SwapMixin = {
swap: function (fun) {
let args = _.rest(arguemnts);
let newValue = fun.apply(this, _.identity);
return this.setValue(newValue);
},
};
let o = {_value: 0, setValue: _.identity}
_.extend(o, SwapMixin);
o.swap(construct, [1, 2, 3]); //[0, 1, 2, 3]

源代码仓库
函数式编程-读书笔记

```
-------------本文结束感谢您的阅读,如果本文对你有帮助就记得给个star-------------
Donate comment here