自定义数组的方法

<body>
    <script>
      // 自己定义 数组扩展方法  求和 和 最大值
      // console.dir(Array);
      // console.log(Array.prototype);
      // Array.prototype.Max = function () {
      //   console.log(this); //[1, 4, 7, 8, 9,17]实例化对象
      //   return Math.max(...this);
      // };
      // let arr = [1, 4, 7, 8, 9, 17];
      // let res = arr.Max();
      // console.log(res);
      // Array.prototype.Total = function () {
      //   // 方法2
      //   // let sum = 0;
      //   // this.map(function (item) {
      //   //   console.log(item);
      //   //   sum += item;
      //   // });
      //   // return sum;
      //   // 方法3
      //   const res2 = this.reduce(function (prve, item) {
      //     console.log(item);
      //     return prve + item;
      //   }, 0);
      //   return res2;
      // };
      // let arr = [1, 4, 7, 8, 9, 17];
      // let res = arr.Total();
      // console.log(res);

      Array.prototype.Min = function () {
        let min = this[0];
        for (let i = 0; i < this.length; i++) {
          // console.log(this[i]);
          min = min < this[i] ? min : this[i];
        }
        return min;
      };
      let arr = [1, 4, 7, 8, 9, 17];
      let min1 = arr.Min();
      console.log(min1);
      let arr2 = [10, 40, 7, 8, 9, 17];
      let res3 = arr2.Min();
      console.log(res3);
    </script>
  </body>

 

posted @ 2022-11-29 20:10  噢噢噢J  阅读(33)  评论(0编辑  收藏  举报