# 深入理解 Monad
# 实现 join 方法
MayBe.prototype.join = function () {
return this.isNothing() ? MayBe.of(null) : this.value
}
1
2
3
2
3
使用:
let joinExample = MayBe.of(MayBe.of(5))
// MayBe {value: MayBe {value: 5}}
joinExample.join()
// MayBe {value: 5}
1
2
3
4
5
2
3
4
5
# 实现 chain
上边总是要在 map 后调用 join,下面把该逻辑封装到一个名为 chain 方法中
MayBe.prototype.chain = function (f) {
return this.map(f).join()
}
1
2
3
2
3