手寫陣列的方法
阿新 • • 發佈:2022-11-29
<body> <script> // map // Array.prototype.Mymap = function (callback) { // let newArr = [] // // console.log(this); // arr [1,5,6] // for (let i = 0; i < this.length; i++) { // newArr.push(callback(this[i], i, this)) // } // return newArr //// callback() // } // let arr = [1, 5, 6] // arr.Mymap(function (item, index, array) { // }) // console.log(res) // Array.prototype.myForEach = function (callback) { // // console.log(this); // [1,5,6] // // console.log(function(){}); // for (let i = 0; i < this.length; i++) {// callback(this[i], i, this) // } // } // let arr = [1, 5, 6] // const res = arr.myForEach(function (item, index, array) { // // console.log(item); // // console.log(index); // console.log(array) // // return 1 // }) // console.log(res) </script> </body>