什么是柯理化 :

    • 当一个函数有多个参数的时候,可以先传递一部分参数调用它(这部分参数以后永远不变);
    • 然后返回一个新的函数接受剩余的参数,返回结果。
    1. // 硬编码
    2. const checkAge = age =>{
    3. const limit = 18;
    4. return age >= limit
    5. }
    6. // 取消硬编码,并且是纯函数
    7. const checkAgePure = (limit , age)=> age >= limit;
    8. console.log(checkAgePure(18,22));
    9. console.log(checkAgePure(18,10));
    10. console.log(checkAgePure(22, 12));
    11. // 18 可能会使用多次,再次优化 => 函数的柯理化
    12. // const checkAgeFn = (limit) =>{
    13. // return (age)=>{
    14. // return age >= limit
    15. // }
    16. // }
    17. // 使用 es6 实现
    18. const checkAge = limit => (age => age >= limit)
    19. const checkAge18 = checkAgeFn(18)
    20. console.log(checkAge18(22))
    21. console.log(checkAge18(12))
    22. console.log(checkAge18(17));
    23. const checkAge22 = checkAgeFn(22)
    24. console.log(checkAge22(22));