lingxiao-Zhu / blog

总结积累,读书笔记
3 stars 0 forks source link

《JavaScript函数式编程指南》 #40

Open lingxiao-Zhu opened 3 years ago

lingxiao-Zhu commented 3 years ago

image

lingxiao-Zhu commented 3 years ago

阅读心得

组合单元 函数 对象、类
编程风格 声明式 命令式
数据和行为 独立且松耦合的纯函数 与方法紧耦合的类
状态管理 将对象是为不可变的值 主张通过实例方法改变对象
程序流控制 函数与递归 循环与条件判断
线程安全 可并发编程 难以实现
封装性 因为一切不可变,不需要封装 需要保护数据的完整性
lingxiao-Zhu commented 3 years ago

重点摘要

// OOP
interface Store {
    getItem(key: string): string;
}

class WebStore implements Store{
    getItem(key: string){
        return localstorage.getItem(key);
    };
}

class WxStore implements Store{
    getItem(key: string){
        return wx.getStorageSync(key);
    };
}

const store = new xxxStore();
store.find(xxx);

// FP
const getFromWebStore = () => {
    return (key)=>{
        return localstorage.getItem(key)
    }
}

const getFromWxStore = () => {
    return (key)=>{
        return wx.getStorageSync(key)
    }
}

const find = getFromXXXStore();
find(xxx);