什么是纯函数?

    1. 纯函数即类似于数学上的函数, 相同的输入始终得到相同的输出, 而且没有任何的副作用, 类似于数学中的 y(x)

    使用纯函数的库有哪些?

    1. Lodash
    2. Folktale
    3. Remad

    数组中的slice和splice有什么区别?

    1. slice 返回数组中指定的部分,不会改变原数组
    2. splice返回数组中指定的部分,会改变原数组 ```javascript /**
      • @desc 纯函数: 相同的输入始终会得到相同的输出 */

    // slice函数是纯函数 不会改变原数组,始终输出相同的值 let arr = [1, 2, 3, 4, 5]; console.log(arr.slice(0, 3)); console.log(arr.slice(0, 3)); console.log(arr.slice(0, 3));

    // splice函数不是纯函数,因为会修改纯数组,每次输出的值都不一样 console.log(arr.splice(0, 3)); console.log(arr.splice(0, 3)); console.log(arr.splice(0, 3));

    // 自定义纯函数 function getSum(n1, n2) { return n1 + n2; } console.log(getSum(1,2)) console.log(getSum(1,2)) console.log(getSum(1,2))

    1. 模拟Lodashmemoize函数
    2. ```javascript
    3. /**
    4. * @desc 演示Lodash
    5. *
    6. */
    7. const _ = require("lodash");
    8. // const arr = ['jack', 'tom', 'rows']
    9. // console.log(_.first(arr))
    10. // console.log(_.last(arr))
    11. // console.log(_.toUpper(_.first(arr)));
    12. // console.log(_.reverse(arr));
    13. // const r = _.each(arr, (item, index) =>{
    14. // console.log(item, index);
    15. // })
    16. // console.log(r)
    17. // 记忆函数
    18. function getArea(r) {
    19. console.log('r', r);
    20. return Math.PI * r * r;
    21. }
    22. const getAreaWithMemoize = _.memoize(getArea);
    23. console.log(getAreaWithMemoize(4))
    24. console.log(getAreaWithMemoize(4))
    25. console.log(getAreaWithMemoize(4))
    26. console.log(getAreaWithMemoize(4))
    27. /**
    28. *
    29. * @desc 模拟Lodash的memoize函数
    30. * @param {function} fn
    31. */
    32. function memoize(fn) {
    33. if(typeof fn !== 'function'){
    34. throw new TypeError('Expected a function');
    35. }
    36. let cache = new Map();
    37. return function(...args) {
    38. const key = args ? args[0] : '';
    39. if(cache.has(key)){
    40. return cache.get(key)
    41. }
    42. const result = fn.apply(this, args);
    43. cache = cache.set(key, result) || cache
    44. return result;
    45. }
    46. }
    47. const getAreaWithMemoize = memoize(getArea);
    48. console.log(getAreaWithMemoize(4))
    49. console.log(getAreaWithMemoize(4))
    50. console.log(getAreaWithMemoize(4))

    Lodash的memoize源码

    1. /**
    2. * @desc Lodash的memoize函数源码实现
    3. */
    4. // function memoize(fn, resolver) {
    5. // if(typeof fn !== 'function' || (resolver != null && typeof resolver !== 'function')){
    6. // throw new TypeError('Expected a function');
    7. // }
    8. // let cache = new Map();
    9. // const memoized = function(...args){
    10. // const key = resolver ? resolver.apply(resolver, args) : args[0]
    11. // // console.log('key', key) // 4
    12. // // const cache = memoized.cache;
    13. // // console.log('cache', cache) // Map对象
    14. // if(cache.has(key)){
    15. // return cache.get(key)
    16. // }
    17. // const result = fn.apply(fn, args)
    18. // // console.log('result', result) // result 50.26548245743669
    19. // cache = cache.set(key, result) || cache
    20. // return result
    21. // }
    22. // // memoized.cache = new (memoize.cache || Map)
    23. // return memoized;
    24. // }
    25. // const getAreaWithMemoize = memoize(getArea);
    26. // console.log(getAreaWithMemoize(4))
    27. // console.log(getAreaWithMemoize(4))
    28. // console.log(getAreaWithMemoize(4))