迭代器 -》实现原理

<!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>Document</title> </head> <body>     <script>         // 遍历器-》迭代器   // iterator 实现原理         // class Iterator{         //     constructor(arr){         //         this.arr = arr;         //         this.index = 0;         //     }         //     next(){         //         let index = this.index,         //             arr = this.arr;
        //         if(index > arr.length - 1){         //             return {         //                 done: true,         //                 value: undefined         //             }         //         } 
        //         return {         //             done: false,         //             value: arr[this.index++]          //         }                        //     };         // }
        // let arr = [10,20,30];         // let itor = new Iterator(arr);         // console.log(itor.next());  // {done: false, value: 10}         // console.log(itor.next());  // {done: false, value: 20}         // console.log(itor.next());  // {done: false, value: 30}         // console.log(itor.next());  // {done: true, value: undefined}
                // let arr = [10,20,30];         // arr[Symbol.iterator] = function(){         //     let index = 0,         //         self = this;          //     return {         //         next(){         //             if(index > self.length -1){         //                 return {         //                     done:true,         //                     value:undefined         //                 }         //             }         //             return {         //                 done:false,         //                 value:self[index++]         //             }         //         }         //     }         // }         // for(let item of arr){         //     console.log(item);         // }

        /**          *  对象类数组使用for of 遍历         */
        // let obj = {         //     0:10,         //     1:20,         //     2:30,         //     3:40,         //     length:4,         //     [Symbol.iterator]:Array.prototype[Symbol.iterator]         //     // [Symbol.iterator]:function(){         //     //     let index = 0,         //     //         self = this;          //     //     return {         //     //         next(){         //     //             return index > self.length -1 ? {         //     //                 done:true,         //     //                 value:undefined         //     //             } : {         //     //                 done:false,         //     //                 value: self[index++]         //     //             }         //     //         }         //     //     };         //     // }         // }
        // for(let item of obj){         //     console.log(item);         // }

        // Object.prototype.sex = '男';         // Object.prototype[Symbol.iterator] = function(){         //     let self = this,         //             index = 0,         //             keys = [         //                 ...Object.getOwnPropertyNames(self),         //                 ...Object.getOwnPropertySymbols(self)         //             ];           //         return {         //             next(){         //                return index > keys.length - 1 ? {         //                     done:true,         //                     value:undefined         //                } : {         //                     done:false,         //                     value:self[keys[index++]]         //                }         //             }         //         }              // }
        // // 遍历对象         // let obj = {         //     name:'Eric',         //     age:11,         //     [Symbol('AA')]:100          // }
        // for(let item of obj){         //     console.log(item);         // }
        // console.log(obj);
 
    </script> </body> </html>
上一篇:归纳循环语句脚本练习


下一篇:Linux - 流程控制语句