{"id":191858,"date":"2020-05-30T11:23:05","date_gmt":"2020-05-30T03:23:05","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=191858"},"modified":"2020-05-20T09:26:12","modified_gmt":"2020-05-20T01:26:12","slug":"es6-iterator-introduction","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/es6-iterator-introduction.html","title":{"rendered":"ES6 \u8fed\u4ee3\u5668\u4ecb\u7ecd"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nIterator \u662f ES6 \u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u904d\u5386\u673a\u5236\uff0c\u8fed\u4ee3\u5668\u6709\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a\u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u4f7f\u5404\u79cd\u6570\u636e\u7ed3\u6784\u53ef\u88ab\u4fbf\u6377\u7684\u8bbf\u95ee\uff0c\u5b83\u662f\u901a\u8fc7\u4e00\u4e2a\u952e\u4e3aSymbol.iterator \u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002
\n\u8fed\u4ee3\u5668\u662f\u7528\u4e8e\u904d\u5386\u6570\u636e\u7ed3\u6784\u5143\u7d20\u7684\u6307\u9488\uff08\u5982\u6570\u636e\u5e93\u4e2d\u7684\u6e38\u6807\uff09\u3002
\n<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

Iterator<\/strong><\/div>\n

Iterator \u662f ES6 \u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u904d\u5386\u673a\u5236\uff0c\u8fed\u4ee3\u5668\u6709\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a<\/p>\n

    \n
  1. \u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u4f7f\u5404\u79cd\u6570\u636e\u7ed3\u6784\u53ef\u88ab\u4fbf\u6377\u7684\u8bbf\u95ee\uff0c\u5b83\u662f\u901a\u8fc7\u4e00\u4e2a\u952e\u4e3aSymbol.iterator \u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/li>\n
  2. \u8fed\u4ee3\u5668\u662f\u7528\u4e8e\u904d\u5386\u6570\u636e\u7ed3\u6784\u5143\u7d20\u7684\u6307\u9488\uff08\u5982\u6570\u636e\u5e93\u4e2d\u7684\u6e38\u6807\uff09\u3002<\/li>\n<\/ol>\n
    \u8fed\u4ee3\u8fc7\u7a0b<\/strong><\/div>\n

    \u8fed\u4ee3\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n

      \n
    1. \u901a\u8fc7 Symbol.iterator \u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u6307\u5411\u5f53\u524d\u6570\u636e\u7ed3\u6784\u7684\u8d77\u59cb\u4f4d\u7f6e<\/li>\n
    2. \u968f\u540e\u901a\u8fc7 next \u65b9\u6cd5\u8fdb\u884c\u5411\u4e0b\u8fed\u4ee3\u6307\u5411\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c next \u65b9\u6cd5\u4f1a\u8fd4\u56de\u5f53\u524d\u4f4d\u7f6e\u7684\u5bf9\u8c61\uff0c\u5bf9\u8c61\u5305\u542b\u4e86 value \u548c done \u4e24\u4e2a\u5c5e\u6027\uff0c value \u662f\u5f53\u524d\u5c5e\u6027\u7684\u503c\uff0c done \u7528\u4e8e\u5224\u65ad\u662f\u5426\u904d\u5386\u7ed3\u675f<\/li>\n
    3. \u5f53 done \u4e3a true \u65f6\u5219\u904d\u5386\u7ed3\u675f<\/li>\n<\/ol>\n

      \u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u8fdb\u884c\u8bf4\u660e\uff1a<\/p>\n

      const items = [\"zero\", \"one\", \"two\"];\r\nconst it = items[Symbol.iterator]();\r\n \r\nit.next();\r\n>{value: \"zero\", done: false}\r\nit.next();\r\n>{value: \"one\", done: false}\r\nit.next();\r\n>{value: \"two\", done: false}\r\nit.next();\r\n>{value: undefined, done: true}<\/pre>\n

      \u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u7136\u540e\u901a\u8fc7 Symbol.iterator \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u4e4b\u540e\u4e0d\u65ad\u7684\u8c03\u7528 next \u65b9\u6cd5\u5bf9\u6570\u7ec4\u5185\u90e8\u9879\u8fdb\u884c\u8bbf\u95ee\uff0c\u5f53\u5c5e\u6027 done \u4e3a true \u65f6\u8bbf\u95ee\u7ed3\u675f\u3002<\/p>\n

      \u8fed\u4ee3\u5668\u662f\u534f\u8bae\uff08\u4f7f\u7528\u5b83\u4eec\u7684\u89c4\u5219\uff09\u7684\u4e00\u90e8\u5206\uff0c\u7528\u4e8e\u8fed\u4ee3\u3002\u8be5\u534f\u8bae\u7684\u4e00\u4e2a\u5173\u952e\u7279\u6027\u5c31\u662f\u5b83\u662f\u987a\u5e8f\u7684\uff1a\u8fed\u4ee3\u5668\u4e00\u6b21\u8fd4\u56de\u4e00\u4e2a\u503c\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u53ef\u8fed\u4ee3\u6570\u636e\u7ed3\u6784\u662f\u975e\u7ebf\u6027\u7684\uff08\u4f8b\u5982\u6811\uff09\uff0c\u8fed\u4ee3\u5c06\u4f1a\u4f7f\u5176\u7ebf\u6027\u5316\u3002<\/p>\n

      \u53ef\u8fed\u4ee3\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/div>\n

      \u4ee5\u4e0b\u662f\u53ef\u8fed\u4ee3\u7684\u503c:<\/p>\n

        \nArray
        \nString
        \nMap
        \nSet
        \nDom\u5143\u7d20\uff08\u6b63\u5728\u8fdb\u884c\u4e2d\uff09<\/ol>\n

        \u6211\u4eec\u5c06\u4f7f\u7528 for...of \u5faa\u73af\uff08\u53c2\u89c1\u4e0b\u6587\u7684 for...of \u5faa\u73af\uff09\u5bf9\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u8fed\u4ee3\u3002<\/p>\n

        Array<\/strong><\/span><\/div>\n

        \u6570\u7ec4 ( Array ) \u548c\u7c7b\u578b\u6570\u7ec4 ( TypedArray ) \u4ed6\u4eec\u662f\u53ef\u8fed\u4ee3\u7684\u3002<\/p>\n

        for (let item of [\"zero\", \"one\", \"two\"]) {\r\n  console.log(item);\r\n}\r\n\/\/ output:\r\n\/\/ zero\r\n\/\/ one\r\n\/\/ two\r\n
        String<\/strong><\/span><\/div><\/pre>\n

        \u5b57\u7b26\u4e32\u662f\u53ef\u8fed\u4ee3\u7684\uff0c\u5355\u4ed6\u4eec\u904d\u5386\u7684\u662f Unicode \u7801\uff0c\u6bcf\u4e2a\u7801\u53ef\u80fd\u5305\u542b\u4e00\u4e2a\u5230\u4e24\u4e2a\u7684 Javascript \u5b57\u7b26\u3002<\/p>\n

        for (const c of 'z\\uD83D\\uDC0A') {\r\n    console.log(c);\r\n}\r\n\/\/ output:\r\n\/\/ z\r\n\/\/ \\uD83D\\uDC0A<\/pre>\n
        Map<\/strong><\/span><\/div>\n

        Map \u4e3b\u8981\u662f\u8fed\u4ee3\u5b83\u4eec\u7684 entries \uff0c\u6bcf\u4e2a entry \u90fd\u4f1a\u88ab\u7f16\u7801\u4e3a [key, value] \u7684\u9879\uff0c entries \u662f\u4ee5\u786e\u5b9a\u7684\u5f62\u52bf\u8fdb\u884c\u8fed\u4ee3\uff0c\u5176\u987a\u5e8f\u662f\u4e0e\u6dfb\u52a0\u7684\u987a\u5e8f\u76f8\u540c\u3002<\/p>\n

        const map = new Map();\r\nmap.set(0, \"zero\");\r\nmap.set(1, \"one\");\r\n \r\nfor (let item of map) {\r\n  console.log(item);\r\n}\r\n\/\/ output:\r\n\/\/ [0, \"zero\"]\r\n\/\/ [1, \"one\"]<\/pre>\n

        \u6ce8\u610f\uff1a WeakMaps \u4e0d\u53ef\u8fed\u4ee3<\/p>\n

        Set<\/strong><\/span><\/div>\n

        Set \u662f\u5bf9\u5176\u5143\u7d20\u8fdb\u884c\u8fed\u4ee3\uff0c\u8fed\u4ee3\u7684\u987a\u5e8f\u4e0e\u5176\u6dfb\u52a0\u7684\u987a\u5e8f\u76f8\u540c<\/p>\n

        const set = new Set();\r\nset.add(\"zero\");\r\nset.add(\"one\");\r\n \r\nfor (let item of set) {\r\n  console.log(item);\r\n}\r\n\/\/ output:\r\n\/\/ zero\r\n\/\/ one<\/pre>\n

        \u6ce8\u610f\uff1a WeakSets \u4e0d\u53ef\u8fed\u4ee3<\/p>\n

        arguments<\/strong><\/span><\/div>\n

        arguments \u76ee\u524d\u5728 ES6 \u4e2d\u4f7f\u7528\u8d8a\u6765\u8d8a\u5c11\uff0c\u4f46\u4e5f\u662f\u53ef\u904d\u5386\u7684<\/p>\n

        function args() {\r\n  for (let item of arguments) {\r\n    console.log(item);\r\n  }\r\n}\r\nargs(\"zero\", \"one\");\r\n\/\/ output:\r\n\/\/ zero\r\n\/\/ one<\/pre>\n
        \u666e\u901a\u5bf9\u8c61\u4e0d\u53ef\u8fed\u4ee3<\/strong><\/div>\n

        \u666e\u901a\u5bf9\u8c61\u662f\u7531 object \u521b\u5efa\u7684\uff0c\u4e0d\u53ef\u8fed\u4ee3\uff1a<\/p>\n

        \/\/ TypeError\r\nfor (let item of {}) { \r\n  console.log(item);\r\n}<\/pre>\n
        for...of\u5faa\u73af<\/strong><\/div>\n

        for...of \u662f ES6 \u65b0\u5f15\u5165\u7684\u5faa\u73af\uff0c\u7528\u4e8e\u66ff\u4ee3 for..in \u548c forEach() \uff0c\u5e76\u4e14\u652f\u6301\u65b0\u7684\u8fed\u4ee3\u534f\u8bae\u3002\u5b83\u53ef\u7528\u4e8e\u8fed\u4ee3\u5e38\u89c4\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982 Array \u3001 String \u3001 Map \u548c Set \u7b49\u7b49\u3002<\/p>\n

        \u8fed\u4ee3\u5e38\u89c4\u6570\u636e\u7c7b\u578b<\/strong><\/div>\n
        Array<\/strong><\/span><\/div>\n
        const nums = [\"zero\", \"one\", \"two\"];\r\n \r\nfor (let num of nums) {\r\n  console.log(num);\r\n}\r\nTypedArray\r\nconst typedArray1 = new Int8Array(6);\r\ntypedArray1[0] = 10;\r\ntypedArray1[1] = 11;\r\n \r\nfor (let item of typedArray1) {\r\n  console.log(item);\r\n}<\/pre>\n
        String<\/strong><\/span><\/div>\n
        const str = \"zero\";\r\n \r\nfor (let item of str) {\r\n  console.log(item);\r\n}<\/pre>\n
        Map<\/strong><\/span><\/div>\n
        let myMap = new Map();\r\nmyMap.set(0, \"zero\");\r\nmyMap.set(1, \"one\");\r\nmyMap.set(2, \"two\");\r\n \r\n\/\/ \u904d\u5386 key \u548c value\r\nfor (let [key, value] of myMap) {\r\n  console.log(key + \" = \" + value);\r\n}\r\nfor (let [key, value] of myMap.entries()) {\r\n  console.log(key + \" = \" + value);\r\n}\r\n \r\n\/\/ \u53ea\u904d\u5386 key\r\nfor (let key of myMap.keys()) {\r\n  console.log(key);\r\n}\r\n \r\n\/\/ \u53ea\u904d\u5386 value\r\nfor (let value of myMap.values()) {\r\n  console.log(value);\r\n}<\/pre>\n
        Set<\/strong><\/span><\/div>\n
        let mySet = new Set();\r\nmySet.add(\"zero\");\r\nmySet.add(\"one\");\r\nmySet.add(\"two\");\r\n \r\n\/\/ \u904d\u5386\u6574\u4e2a set\r\nfor (let item of mySet) {\r\n  console.log(item);\r\n}\r\n \r\n\/\/ \u53ea\u904d\u5386 key \u503c\r\nfor (let key of mySet.keys()) {\r\n  console.log(key);\r\n}\r\n \r\n\/\/ \u53ea\u904d\u5386 value\r\nfor (let value of mySet.values()) {\r\n  console.log(value);\r\n}\r\n \r\n\/\/ \u904d\u5386 key \u548c value \uff0c\u4e24\u8005\u4f1a\u76f8\u7b49\r\nfor (let [key, value] of mySet.entries()) {\r\n  console.log(key + \" = \" + value);\r\n}<\/pre>\n
        \u53ef\u8fed\u4ee3\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/div>\n

        of \u64cd\u4f5c\u6570\u5fc5\u987b\u662f\u53ef\u8fed\u4ee3\uff0c\u8fd9\u610f\u5473\u7740\u5982\u679c\u662f\u666e\u901a\u5bf9\u8c61\u5219\u65e0\u6cd5\u8fdb\u884c\u8fed\u4ee3\u3002\u5982\u679c\u6570\u636e\u7ed3\u6784\u7c7b\u4f3c\u4e8e\u6570\u7ec4\u7684\u5f62\u5f0f\uff0c\u5219\u53ef\u4ee5\u501f\u52a9 Array.from() \u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\u8fed\u4ee3\u3002<\/p>\n

        const arrayLink = {length: 2, 0: \"zero\", 1: \"one\"}<\/pre>\n
        \/\/ \u62a5 TypeError \u5f02\u5e38\r\nfor (let item of arrayLink) {\r\n  console.log(item);\r\n}\r\n \r\n\/\/ \u6b63\u5e38\u8fd0\u884c\r\n\/\/ output:\r\n\/\/ zero\r\n\/\/ one\r\nfor (let item of Array.from(arrayLink)) {\r\n  console.log(item);\r\n}<\/pre>\n

        let \u3001const \u548c var \u7528\u4e8e for..of<\/strong><\/p>\n

        \u5982\u679c\u4f7f\u7528 let \u548c const \uff0c\u6bcf\u6b21\u8fed\u4ee3\u5c06\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u8fd9\u53ef\u4ee5\u4fdd\u8bc1\u4f5c\u7528\u57df\u5728\u8fed\u4ee3\u7684\u5185\u90e8\u3002<\/p>\n

        const nums = [\"zero\", \"one\", \"two\"];\r\n \r\nfor (const num of nums) {\r\n  console.log(num);\r\n}\r\n\/\/ \u62a5 ReferenceError\r\nconsole.log(num);<\/pre>\n

        \u4ece\u4e0a\u9762\u7684\u4f8b\u5b50\u6211\u4eec\u770b\u5230\uff0c\u6700\u540e\u4e00\u53e5\u4f1a\u62a5\u5f02\u5e38\uff0c\u539f\u56e0 num \u7684\u4f5c\u7528\u57df\u53ea\u5728\u5faa\u73af\u4f53\u5185\u90e8\uff0c\u5916\u90e8\u65e0\u6548\uff0c\u5177\u4f53\u53ef\u67e5\u9605 let \u4e0e const \u7ae0\u8282\u3002\u4f7f\u7528 var \u5219\u4e0d\u4f1a\u51fa\u73b0\u4e0a\u8ff0\u60c5\u51b5\uff0c\u56e0\u4e3a var \u4f1a\u4f5c\u7528\u4e8e\u5168\u5c40\uff0c\u8fed\u4ee3\u5c06\u4e0d\u4f1a\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b58\u50a8\u7a7a\u95f4\u3002<\/p>\n

        const nums = [\"zero\", \"one\", \"two\"];\r\n \r\nforv (var num of nums) {\r\n  console.log(num);\r\n}\r\n\/\/ output: two\r\nconsole.log(num);<\/pre>\n","protected":false},"excerpt":{"rendered":"

        Iterator \u662f ES6 \u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u904d\u5386\u673a\u5236\uff0c\u8fed\u4ee3\u5668\u6709\u4e24\u4e2a\u6838\u5fc3\u6982\u5ff5\uff1a \u8fed\u4ee3\u5668\u662f\u4e00\u4e2a\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u5b83\u7684\u4f5c\u7528 […]<\/p>\n","protected":false},"author":1470,"featured_media":191860,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-191858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=191858"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191858\/revisions"}],"predecessor-version":[{"id":191868,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/191858\/revisions\/191868"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/191860"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=191858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=191858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=191858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}