纯函数默写第一遍

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>纯函数默写第一遍</title>
  </head>
  <body>
    <script>
      function getArea(r) {
        return Math.PI * r * r;
      }
      function memorize(func) {
        let obj = {};
        return function () {
          let key = JSON.stringify([...arguments]);
          if (!obj[key]) {
            console.log(123);
            obj[key] = func(...arguments);
          }
          return obj[key];
        };
      }
      let getOneArea = memorize(getArea);
      console.log(getOneArea(5));
      console.log(getOneArea(5));
      console.log(getOneArea(5));
      console.log(getOneArea(5));
      console.log(getOneArea(5));
      console.log(getOneArea(4));
    </script>
  </body>
</html>

 

posted @ 2022-04-05 10:43  苹果π  阅读(9)  评论(0编辑  收藏  举报